Ad

How To Change The Json Data Into Required Json

- 1 answer

Hey guys i am trying to change the Json data to my required Json and store it into a new Json. My Json data is

var dataSet = {"ContractNumber":["17102367","17054217","17835641","17597810","17131176","17592158","17601558","17457472","17843672","17555883"],
"ContractType":["CP4","USV","EAT","UNI","UPS","UPS","UPS","UPS","UPS","UPS","123"]}

required Json in

var dataSet =[
["17102367", "CP4"],
["17054217", "UNI"],
["17054666", "UN3"],
["17054217", "U23"],
["17102367","CP4"],
["17054217","USV"],
["17835641","EAT"],
["17597810","UNI"],
["17131176","UPS"],
["17592158","UPS"],
["17601558","UPS"],
["17457472","UPS"],
["17843672","UPS"],
["17555883","UPS"],
["","123"]
]
Ad

Answer

use array map function

var dataSet = {"ContractNumber":["17102367","17054217","17835641","17597810","17131176","17592158","17601558","17457472","17843672","17555883"],
"ContractType":["CP4","USV","EAT","UNI","UPS","UPS","UPS","UPS","UPS","UPS","123"]}

dataSet = dataSet.ContractType.map(function(ct, index) {
    return [dataSet.ContractNumber[index] || '', ct];
});

The last value will be

['', '123']

because

[, '123']

is not valid javascript

edit: the [, '123'] actually is valid javascript, my bad

Ad
source: stackoverflow.com
Ad