Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
problem is that. I have one json object, lets assume it's look like that:
{
"first": {
"arg": [
"val1",
"val2"
]
},
"second": {
"arg1": [
"val11",
"val12"
],
"arg2": [
"val21",
"val22"
]
}
}
And now I need to make from it two objects , looking like this:
{
"first": {
"arg":"val1"
},
"second": {
"arg1":"val11",
"arg2":"val21"
}
}
And second one like this:
{
"first": {
"arg":"val2"
},
"second": {
"arg1":"val12",
"arg2":"val22"
}
}
There is easy way to make them like that?
You could map the most inner values or the mapped valued from the recursive call and assign the value to an object by respecting the index.
function map(object) {
return Object
.entries(object)
.reduce((r, [k, v]) => {
(Array.isArray(v) ? v : map(v)).forEach((w, i) => {
r[i] = r[i] || {};
r[i][k] = w;
});
return r;
}, []);
}
var data = { first: { arg: ["val1", "val2"] }, second: { arg1: ["val11", "val12"], arg2: ["val21", "val22"] } },
result = map(data);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
i have an array like below
[
{
"202229": "8.418"
},
{
"202229": null
},
{
"202229": null
},
{
"202230": "10.713"
},
{
"202230": "0.859"
}
]
i want to convert it to below structure
[
{
"202229": "8.418",
"202229": null,
"202229": null,
"202230": "10.713",
"202230": "0.859"
}
]
Note that values are not overwritten and keys "202229" etc are dynamic . I tried using reduce methods but i couldn't get it in this format . Any help would be appreciated . Thanks
You could disperse the keys to various objects.
const
data = [{ 202229: "8.418" }, { 202229: null }, { 202229: null }, { 202230: "10.713" }, { 202230: "0.859" }, { 202231: "0.503" }, { 202231: null }],
result = data.reduce((r, o) => {
Object.entries(o).forEach(([k, v]) => {
let i = 0;
while (k in (r[i] ??= {})) i++;
r[i][k] = v;
});
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 months ago.
Improve this question
I want to convert data object from:
let data = {
"data.name" : "michael",
"data.email" : "michael#xsx.com",
"person.gender" : "male"
}
into
{
"data" : {
"name": "michael",
"email": "michael#xsx.com"
},
"person" : {
"gender": "male"
}
}
I tried a loop and trying to split key:
let newdata = new Object()
for (const property in datas) {
let key = `${property.split(".")[0]}`
let subkey = `${property.split(".")[1]}`
newdata['key'] = key;
newdata['value'] = {subkey: `${datas[property]}`};
console.log(newdata);
}
The result looks like this:
"data" : {
"name" : {
"michael"
}
},
"data" : {
"email" : {
"michael#xsxx.com"
}
},
"person" : {
"gender" : {
"male"
}
}
What's next?
You can use Object.entries to get the key/value pairs from your existing data, then use reduce to convert that into a new object with the split keys forming a nested object:
const data = {
"data.name" : "michael",
"data.email" : "michael#xsx.com",
"person.gender" : "male"
}
const result = Object.entries(data).reduce((acc, [key, value]) => {
[key, subkey] = key.split('.')
acc[key] = acc[key] || {}
acc[key][subkey] = value
return acc
}, {})
console.log(result)
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I have a JSON object in the below format which I want to convert into a JSON array. I have tried multiple ways to achieve this, but I can not get success.
{
"name":{
"0":"mike",
"1":"george",
"2":"Andrew"
},
"category":{
"0":"A",
"1":"B",
"2":"C"
}
}
The output would be like this:
{
"0":{
"name":"mike",
"category":"A"
},
"1":{
"name":"george",
"category":"B"
},
"2":{
"name":"andrew",
"category":"C"
}
}
I am new to JSON. How can I achieve this?
You can make use of Object.keys and Object.entries
const obj = {
name: {
"0": "mike",
"1": "george",
"2": "Andrew",
},
category: {
"0": "A",
"1": "B",
"2": "C",
},
};
const props = Object.keys(obj);
const result = props.reduce((acc, key) => {
Object.entries(obj[key]).forEach(([k, v]) => {
if (!acc[k]) acc[k] = Object.fromEntries(props.map((p) => [p, ""]));
acc[k][key] = v;
});
return acc;
}, {});
console.log(result);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have an object like this
{ '2020-08': {Invoice: 21400, Receipt: 20800, ...},
'2020-09': {Invoice: 8003.6, ...},
'2020-10': {Receipt: 7779.2, ...},
'2020-11': {Invoice: 32970, ...}
}
Wanna make it to be 2d array like this
[ ['2020-08', '2020-09', '2020-10', '2020-11'],
[ 21400 , 8003.6 , 0 , 32970 ], //Invoice
[ 20800 , 0 , 7779.2 , 0 ], //Receipt
...
]
It is very easy to achieve using Object.keys.
const data = {
'2020-08': {
Invoice: 21400,
Receipt: 20800
},
'2020-09': {
Invoice: 8003.6,
Receipt: 7779
},
'2020-10': {
Receipt: 7779.2,
Invoice: 32970
},
'2020-11': {
Invoice: 32970,
}
}
const res = []
const keys = [...Object.keys(data)]
const d = Object.keys(data).map(item => {
return data[item]["Invoice"] || 0
})
const d1 = Object.keys(data).map(item => {
return data[item]["Receipt"] || 0
})
console.log([keys, d, d1])
Try:
let result = [], key= [], invoice= [], receipt = [];
Object.keys(obj).forEach((x) => {
key.push(x);
invoice.push(obj[x].Invoice || 0);
receipt.push(obj[x].Receipt || 0);
});
result = [key,invoice,receipt];
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to convert the array to map,
The array looks like :
var array = [{
"id" : 123
}, {
"id" : 456
}, {
"id" : 789
}];
The final object I'm trying to build should look like :
var result = {
"123": { id: 123 } ,
"456": { id: 456 } ,
"789": { id: 789 }
};
Any efficient way to implement it will be appreciated :)
Thanks
var array = [
{
"id": 123,
"otherProp": "true"
},
{
"id": 456,
"otherProp": "false"
},
{
"id": 789,
"otherProp": "true"
}
];
var result = array.reduce(function (acc, cur, i) {
acc[cur.id] = cur;
return acc;
}, {});
console.log(result);
Use javaScript reduce
The reduce() method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.
Use reduce
var array = [{
"id" : 123
}, {
"id" : 456
}, {
"id" : 789
}];
var expectedValue = {
"123": { id: 123 } ,
"456": { id: 456 } ,
"789": { id: 789 }
};
var result = array.reduce( (acc, c) => (acc[ c.id ] = c, acc) ,{});
console.log('result : ', result);
console.log('(JSON.stringify(expectedValue) === JSON.stringify(result)) ? ', (JSON.stringify(expectedValue) === JSON.stringify(result)));
.as-console-wrapper { max-height: 100%!important; top: 0; }
Explanation
Use reduce to iterate and initialize the accumulator to {}
Set the key as id of item of every iteration c and value as c itself.