how to merge similar key values in same keys in javascript [closed] - javascript

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 2 years ago.
Improve this question
var array = [{
"ADJUSTMENT_TYPE": "AP0001",
"REV_TYPE": "R"
}, {
"ADJUSTMENT_TYPE": "AP0001",
"REV_TYPE": "E"
}, {
"ADJUSTMENT_TYPE": "AP0001",
"REV_TYPE": "N"
}];
I want it to be like this
[{
"ADJUSTMENT_TYPE": "AP0001",
"REV_TYPE": "R",
"E",
"N"
}]
I want this in javascript. can anyone help me
thanks in advance

Current example does not check for duplicates REV_TYPE:
const array = [
{ ADJUSTMENT_TYPE: 'AP0001', REV_TYPE: 'R' },
{ ADJUSTMENT_TYPE: 'AP0002', REV_TYPE: 'E' },
{ ADJUSTMENT_TYPE: 'AP0001', REV_TYPE: 'N' },
{ ADJUSTMENT_TYPE: 'AP0001', REV_TYPE: 'N' },
{ ADJUSTMENT_TYPE: 'AP0002', REV_TYPE: 'N' },
{ ADJUSTMENT_TYPE: 'AP0002', REV_TYPE: 'N' },
];
const result = array.reduce((acc, val) => {
const findType = acc.find(t => t.ADJUSTMENT_TYPE === val.ADJUSTMENT_TYPE);
if (findType) {
findType.REV_TYPE.push(val.REV_TYPE);
} else {
acc.push({ ADJUSTMENT_TYPE: val.ADJUSTMENT_TYPE, REV_TYPE: [val.REV_TYPE] });
}
return acc;
}, []);
console.log(result);

Related

Combine Objects in a js array without overwriting them [closed]

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; }

Convert a JSON Object to an index based JSON Array in JavaScript [closed]

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; }

Vue JS create json objects from one json object [closed]

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; }

Create an object from array [closed]

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.

How to convert object to array of objects javascript [closed]

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 7 years ago.
Improve this question
I have the following object data which looks like this:
data = {
anna:{
phase1:23,
phase2:24,
phase3:0,
phase4:5,
phase5:0
},
Robin:{
phase1:16,
phase2:12,
phase3:21,
phase4:23,
phase5:2
}
}
Now I wanted to convert them to where data variable is object and anna and robin is array of objects:
data = {
anna:[
{ phase1: 23 },
{ phase2: 24 },
{ phase3: 0 },
{ phase4: 5 },
{ phase5: 0 }
],
Robin:[
{ phase1: 16 },
{ phase2: 12 },
{ phase3: 21 },
{ phase4: 23 },
{ phase5: 2 }
]
}
A function like below might help you:
EDIT: edited the answer accommodating changes suggested by cafebabe1991.
function convertToArray(obj) {
var retVal = [];
for (var key in obj) { { //iterates through the list of key-val pairs
if (obj.hasOwnProperty(key)) {
retVal.push({ key: obj[key]}); //pushes it to newly created array
}
}
try this
function convertToArray(obj) {
var retVal = [];
for (key in obj) { //iterates through the list of key-val pairs
if (obj.hasOwnProperty(key)) {
retVal.push({ key: obj[key]});
} //pushes it to newly created array
}
return retVal;
}

Categories

Resources