Reputation: 2893
Am trying form data from object array to array of arrays in JavaScript but am unable to get the result
Input Data { "sept" : [1,2,3], "oct" : [5,6,7] "Nov" : [7,8,9]}
Expected Output [["sept",1,2,3],["oct",5,6,7],["Nov",7,8,9]]
I have tried a lot I can able to get the keys by Object.Key()
but can able to form the data with the value as expected output above, please help me to resolve this Thanks in advance
Upvotes: 0
Views: 53
Reputation: 13077
The Ramda.js library has a function that does exactly this called toPairs()
https://ramdajs.com/docs/#toPairs
Upvotes: 0
Reputation: 386560
You could get the entries and map key and values in an array.
const
data = { sept: [1, 2, 3], oct: [5, 6, 7], Nov: [7, 8, 9] },
result = Object.entries(data).map(([k, v]) => [k, ...v]);
console.log(result);
Upvotes: 1
Reputation: 14891
You could use Object.entries
then spread the values along with the key
const data = { sept: [1, 2, 3], oct: [5, 6, 7], Nov: [7, 8, 9] }
const res = Object.entries(data).map(([key, value]) => [key, ...value])
console.log(res)
Upvotes: 1
Reputation: 73
Use Object.entries()
and then .map()
with ...
- rest/spread operator
const object = {
september: [1, 2, 3],
october: [5, 6, 7],
november: [7, 8, 9],
}
const result = Object.entries(object).map(([key, value])=>[key, ...value])
Upvotes: 1
Reputation: 8301
Just iterate over properties using for .. in
loop, you will get the result.
const obj = {
"sept": [1, 2, 3],
"oct": [5, 6, 7],
"Nov": [7, 8, 9],
}
const result = [];
for (const prop in obj) {
result.push([prop, ...obj[prop]]);
}
console.log(result)
Upvotes: 1