amit kaushik
amit kaushik

Reputation: 79

how to Change the Json data into required Json

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"]
]

Upvotes: 1

Views: 63

Answers (1)

Jaromanda X
Jaromanda X

Reputation: 1

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

Upvotes: 4

Related Questions