I have array with nested objects, want to add sum of value and make an object in javascript.
const arr = [
{
preliminary: {
contract_value: 1000,
planned_value: 500,
balance_value: 500,
},
substructure: {
contract_value: 1000,
planned_value: 500,
balance_value: 500,
},
superstructure: {
contract_value: 1000,
planned_value: 500,
balance_value: 500,
},
architectural: {
contract_value: 1000,
planned_value: 500,
balance_value: 500,
},
mande: {
contract_value: 1000,
planned_value: 500,
balance_value: 500,
},
externalwork: {
contract_value: 1000,
planned_value: 500,
balance_value: 500,
},
miscorcontingency: {
contract_value: 1000,
planned_value: 500,
balance_value: 500,
},
},
{
preliminary: {
contract_value: 100,
planned_value: 50,
balance_value: 50,
},
substructure: {
contract_value: 100,
planned_value: 50,
balance_value: 50,
},
superstructure: {
contract_value: 100,
planned_value: 50,
balance_value: 50,
},
architectural: {
contract_value: 100,
planned_value: 50,
balance_value: 50,
},
mande: {
contract_value: 100,
planned_value: 50,
balance_value: 50,
},
externalwork: {
contract_value: 100,
planned_value: 50,
balance_value: 50,
},
miscorcontingency: {
contract_value: 100,
planned_value: 50,
balance_value: 50,
},
},
];
I have done for one object, so want do multiple object
const result = arr.reduce(
(obj, { preliminary, ...rest }) => {
return (
Object.keys(preliminary).map(
(key) =>
(obj.preliminary[key] =
preliminary[key] (Number(obj.preliminary[key]) || 0))
),
obj
);
},
{
preliminary: {},
substructure: {},
superstructure: {},
architectural: {},
mande: {},
externalwork: {},
miscorcontingency: {},
}
);
console.log(result)
Here I attach the fiddle link
I got out put for "preliminary" value. I need all of object mean needs to do dynamically. Thanks in advance.
CodePudding user response:
We can use Array.reduce()
several times in combination with Object.entries()
to add the properties at the correct level in the structure, the end result should be as required:
const arr = [ { preliminary: { contract_value: 1000, planned_value: 500, balance_value: 500, }, substructure: { contract_value: 1000, planned_value: 500, balance_value: 500, }, superstructure: { contract_value: 1000, planned_value: 500, balance_value: 500, }, architectural: { contract_value: 1000, planned_value: 500, balance_value: 500, }, mande: { contract_value: 1000, planned_value: 500, balance_value: 500, }, externalwork: { contract_value: 1000, planned_value: 500, balance_value: 500, }, miscorcontingency: { contract_value: 1000, planned_value: 500, balance_value: 500, }, }, { preliminary: { contract_value: 100, planned_value: 50, balance_value: 50, }, substructure: { contract_value: 100, planned_value: 50, balance_value: 50, }, superstructure: { contract_value: 100, planned_value: 50, balance_value: 50, }, architectural: { contract_value: 100, planned_value: 50, balance_value: 50, }, mande: { contract_value: 100, planned_value: 50, balance_value: 50, }, externalwork: { contract_value: 100, planned_value: 50, balance_value: 50, }, miscorcontingency: { contract_value: 100, planned_value: 50, balance_value: 50, }, }]
const result = arr.reduce((acc, cur) => {
return Object.entries(cur).reduce((acc, [key1, val1]) => {
return Object.entries(val1).reduce((acc, [key2, val2]) => {
acc[key1] = acc[key1] || {};
acc[key1][key2] = (acc[key1][key2] || 0) val2;
return acc;
}, acc)
}, acc)
}, {})
console.log('Result:',result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
CodePudding user response:
- Using
Array#reduce
, iterate over the array while updating an object accumulator- In every iteration, using
Object#keys
, iterate over the keys of the object at hand- Set
prev
to the the value of the key saved in the accumulator if exists, andcurrent
to the value in the object at hand - If no previous value was saved for this key in the accumulator set the object to it
- Otherwise, using
Object#keys
andArray#forEach
, iterate over its keys and update the accumulator
- Set
- In every iteration, using
const arr = [
{ preliminary: { contract_value: 1000, planned_value: 500, balance_value: 500 },
substructure: { contract_value: 1000, planned_value: 500, balance_value: 500 },
superstructure: { contract_value: 1000, planned_value: 500, balance_value: 500 },
architectural: { contract_value: 1000, planned_value: 500, balance_value: 500 },
mande: { contract_value: 1000, planned_value: 500, balance_value: 500 },
externalwork: { contract_value: 1000, planned_value: 500, balance_value: 500 },
miscorcontingency: { contract_value: 1000, planned_value: 500, balance_value: 500 }
},
{
preliminary: { contract_value: 100, planned_value: 50, balance_value: 50 },
substructure: { contract_value: 100, planned_value: 50, balance_value: 50 },
superstructure: { contract_value: 100, planned_value: 50, balance_value: 50 },
architectural: { contract_value: 100, planned_value: 50, balance_value: 50 },
mande: { contract_value: 100, planned_value: 50, balance_value: 50 },
externalwork: { contract_value: 100, planned_value: 50, balance_value: 50 },
miscorcontingency: { contract_value: 100, planned_value: 50, balance_value: 50 }
}
];
const res = arr.reduce((acc, obj) => {
Object.keys(obj).forEach(key => {
const prev = acc[key], current = obj[key];
if(!prev) acc[key] = current;
else Object.keys(current).forEach(k => prev[k] = (prev[k] || 0) current[k]);
});
return acc;
}, {});
console.log(res);
CodePudding user response:
this is my solution:
const arr = [
{ preliminary: { contract_value: 1000, planned_value: 500, balance_value: 500 },
substructure: { contract_value: 1000, planned_value: 500, balance_value: 500 },
superstructure: { contract_value: 1000, planned_value: 500, balance_value: 500 },
architectural: { contract_value: 1000, planned_value: 500, balance_value: 500 },
mande: { contract_value: 1000, planned_value: 500, balance_value: 500 },
externalwork: { contract_value: 1000, planned_value: 500, balance_value: 500 },
miscorcontingency: { contract_value: 1000, planned_value: 500, balance_value: 500 }
},
{
preliminary: { contract_value: 100, planned_value: 50, balance_value: 50 },
substructure: { contract_value: 100, planned_value: 50, balance_value: 50 },
superstructure: { contract_value: 100, planned_value: 50, balance_value: 50 },
architectural: { contract_value: 100, planned_value: 50, balance_value: 50 },
mande: { contract_value: 100, planned_value: 50, balance_value: 50 },
externalwork: { contract_value: 100, planned_value: 50, balance_value: 50 },
miscorcontingency: { contract_value: 100, planned_value: 50, balance_value: 50 }
}
];
let result = arr.reduce((out, cv, ci)=>{
if(ci == 0) return out;
Object.keys(cv).forEach(key => {
Object.keys(cv[key]).forEach(key2 => {
(out[key] ??= {})[key2] = cv[key][key2];
})
});
return out;
}, arr[0] );
console.log(result)
CodePudding user response:
const arr = [
{
preliminary: {
contract_value: 1000,
planned_value: 500,
balance_value: 500,
},
substructure: {
contract_value: 1000,
planned_value: 500,
balance_value: 500,
},
superstructure: {
contract_value: 1000,
planned_value: 500,
balance_value: 500,
},
architectural: {
contract_value: 1000,
planned_value: 500,
balance_value: 500,
},
mande: {
contract_value: 1000,
planned_value: 500,
balance_value: 500,
},
externalwork: {
contract_value: 1000,
planned_value: 500,
balance_value: 500,
},
miscorcontingency: {
contract_value: 1000,
planned_value: 500,
balance_value: 500,
},
total: {
contract_value: "7000.00",
planned_value: "3500.00",
balance_value: "3500.00",
},
},
{
preliminary: {
contract_value: 100,
planned_value: 50,
balance_value: 50,
},
substructure: {
contract_value: 100,
planned_value: 50,
balance_value: 50,
},
superstructure: {
contract_value: 100,
planned_value: 50,
balance_value: 50,
},
architectural: {
contract_value: 100,
planned_value: 50,
balance_value: 50,
},
mande: {
contract_value: 100,
planned_value: 50,
balance_value: 50,
},
externalwork: {
contract_value: 100,
planned_value: 50,
balance_value: 50,
},
miscorcontingency: {
contract_value: 100,
planned_value: 50,
balance_value: 50,
},
total: {
contract_value: "700.00",
planned_value: "350.00",
balance_value: "350.00",
},
},
];
const result = arr.reduce((obj, outerKeys) => {
Object.keys(outerKeys).map((outerKey) => {
// console.log(`outerKey:::`, outerKey);
// console.log(`values[outerKey]:::`, outerKeys[outerKey]);
// console.log(`obj:::`, obj);
if (obj[outerKey]) {
const tempObject = outerKeys[outerKey];
Object.keys(tempObject).map((objKey) => {
const tempInnerObject = obj[outerKey];
tempInnerObject[objKey] =
Number(tempInnerObject[objKey]) Number(tempObject[objKey]);
// console.log(`objKey:::`, objKey);
// console.log(`tempObject[objKey]:::`, tempObject[objKey]);
});
} else {
obj[outerKey] = outerKeys[outerKey];
}
});
return obj;
}, {});
console.log(`result :::`, result);