Javascript grouping - javascript

How can I add data by grouping with country label and create data array that has 12 index indicating months and containing value of data before grouping.
I need help, how to push and group the data according to the month number. e.x
arr = [
{ label: 'US', data: '10', monthNumber: 1 },
{ label: 'US', data: '2', monthNumber: 3 },
{ label: 'US', data: '60', monthNumber: 2 },
{ label: 'UK', data: '10', monthNumber: 5 },
{ label: 'SA', data: '1', monthNumber: 1 },
{ label: 'CA', data: '70', monthNumber: 1 },
{ label: 'SA', data: '10', monthNumber: 12 },
];
now i need the results to be like
[
{ label: 'US', data: [10,60,2,0,0,0,0,0,0,0,0,0] },
{ label: 'UK', data: [0,0,0,0,10,0,0,0,0,0,0,0] },
{ label: 'SA', data: [1,0,0,0,0,0,0,0,0,0,0,10] },
{ label: 'CA', data: [70,0,0,0,0,0,0,0,0,0,0,0] },
];

Create a new object by reducing over the array using the labels as object keys, and initialising the property value as an object with a label, and a pre-filled array of zeros. Then update the array with the data at the relevant position.
const arr=[{label:"US",data:"10",monthNumber:1},{label:"US",data:"2",monthNumber:3},{label:"US",data:"60",monthNumber:2},{label:"UK",data:"10",monthNumber:5},{label:"SA",data:"1",monthNumber:1},{label:"CA",data:"70",monthNumber:1},{label:"SA",data:"10",monthNumber:12}];
function grouper(arr) {
// `reduce` over the array passing in an
// empty object as the initial accumulator value
const out = arr.reduce((acc, c) => {
// Destructure the properties from the current
// iterated object
const { label, data, monthNumber } = c;
// If the label doesn't exist as a key on the object
// create it, and assign an object as its value, using
// the label, and adding a pre-filled array of zeros to
// its data property
acc[label] ??= { label, data: new Array(12).fill(0) };
// Update the data array with the data value at the
// appropriate position
acc[label].data[monthNumber - 1] = Number(data);
// Return the accumulator for the next iteration
return acc;
}, {});
// Finally get the array of updated objects
// from the accumulated data
return Object.values(out);
}
console.log(grouper(arr));
Additional documentation
Logical nullish assignment
fill
Object.values

For grouping, you could make use of reduce
const arr = [
{ label: "US", data: "10", monthNumber: 1 },
{ label: "US", data: "2", monthNumber: 3 },
{ label: "US", data: "60", monthNumber: 2 },
{ label: "UK", data: "10", monthNumber: 5 },
{ label: "SA", data: "1", monthNumber: 1 },
{ label: "CA", data: "70", monthNumber: 1 },
{ label: "SA", data: "10", monthNumber: 12 },
]
let res = arr.reduce((acc, { label, monthNumber, data }) => {
if (!acc[label]) acc[label] = Array(12).fill(0)
acc[label][monthNumber - 1] = Number(data)
return acc
}, {})
res = Object.entries(res).map(([label, data]) => ({ label, data }))
console.log(res)

First I have extracted the label from the current array.
After that, I have a loop through those labels, and inside that loop through the array, and created an object with store values of data and label.
please see the comments for a better understanding.
Important thing to notice here is that for the same month number for labels value will be replaced with the last one.
arr = [{
label: 'US',
data: '10',
monthNumber: 1
},
{
label: 'US',
data: '2',
monthNumber: 3
},
{
label: 'US',
data: '60',
monthNumber: 2
},
{
label: 'UK',
data: '10',
monthNumber: 5
},
{
label: 'SA',
data: '1',
monthNumber: 1
},
{
label: 'CA',
data: '70',
monthNumber: 1
},
{
label: 'SA',
data: '10',
monthNumber: 12
},
];
const labels = [];
const result = [];
// extracting unique labels
arr.forEach(item => {
!labels.includes(item.label) && labels.push(item.label);
})
// looping through labels
labels.forEach(label => {
// creating empty object
const object = {};
// creating data array with 12 values by default 0
const data = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
// loop though array values
arr.forEach(item => {
// checking if current outer label is matching with array label
if (item.label == label) {
// if abive condition true add label key in object with value of current outer label
object["label"] = label;
// updating month value
data[item.monthNumber - 1] = item.data;
}
// adding data in object with key data.
object["data"] = data;
})
// pushing final object in result
result.push(object);
})
//printing final result
console.log(result);

Good starting point will be to group the data by label first.
Then take the values from that to remap the data.
Create a Month array generating a new array with 0 values
Loop through and add the data to the month array based on MonthNumber
const remap = () => {
let arr = [
{ label: 'US', data: '10', monthNumber: 1 },
{ label: 'US', data: '2', monthNumber: 3 },
{ label: 'US', data: '60', monthNumber: 2 },
{ label: 'UK', data: '10', monthNumber: 5 },
{ label: 'SA', data: '1', monthNumber: 1 },
{ label: 'CA', data: '70', monthNumber: 1 },
{ label: 'SA', data: '10', monthNumber: 12 }
];
return Object.values(
arr.reduce((acc, v) => {
if (!acc.hasOwnProperty(v.label)) {
acc[v.label] = [];
}
acc[v.label].push(v);
return acc;
}, {})
).map((flatData) => {
const label = Array.isArray(flatData) && flatData.length > 0 ? flatData[0].label : '';
const monthArray = new Array(12).fill(0);
flatData.forEach(({ data, monthNumber }) => {
monthArray[monthNumber] = parseInt(data);
});
return { label, data: monthArray };
});
};
console.log(remap())

Try this solution:
function setupGraph(arr){
data = [];
for (let i = 0; i < arr.length; i++) {
let found = false;
for (let j = 0; j < data.length; j++) {
if (data[j].label === arr[i].label) {
data[j].data[arr[i].monthNumber - 1] = Number(arr[i].data);
found = true;
break;
}
}
if (!found) {
data.push({ label: arr[i].label, data: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] });
data[data.length - 1].data[arr[i].monthNumber - 1] = Number(arr[i].data);
}
}
return data;
}
Testing the function:
arr = [
{ label: 'US', data: '10', monthNumber: 1 },
{ label: 'US', data: '2', monthNumber: 3 },
{ label: 'US', data: '60', monthNumber: 2 },
{ label: 'UK', data: '10', monthNumber: 5 },
{ label: 'SA', data: '1', monthNumber: 1 },
{ label: 'CA', data: '70', monthNumber: 1 },
{ label: 'SA', data: '10', monthNumber: 12 },
];
let result = setupGraph(arr)
console.log(result);
[
{
label: 'US',
data: [
10, 60, 2, 0, 0,
0, 0, 0, 0, 0,
0, 0
]
},
{
label: 'UK',
data: [
0, 0, 0, 0, 10,
0, 0, 0, 0, 0,
0, 0
]
},
{
label: 'SA',
data: [
1, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 10
]
},
{
label: 'CA',
data: [
70, 0, 0, 0, 0,
0, 0, 0, 0, 0,
0, 0
]
}
]

const defaultMonthsData = [0,0,0,0,0,0,0,0,0,0,0,0];
const arr = [
{ label: 'US', data: '10', monthNumber: 1 },
{ label: 'US', data: '2', monthNumber: 3 },
{ label: 'US', data: '60', monthNumber: 2 },
{ label: 'UK', data: '10', monthNumber: 5 },
{ label: 'SA', data: '1', monthNumber: 1 },
{ label: 'CA', data: '70', monthNumber: 1 },
{ label: 'SA', data: '10', monthNumber: 12 },
];
const results = _(arr)
.groupBy("label")
.map((value, label) => {
const monthsData = JSON.parse(JSON.stringify(defaultMonthsData));
return {
label,
data: _.map(value, function(o, index) {
monthsData[o.monthNumber] = parseInt(o.data);
return monthsData
})[0],
}
}).value();
console.log(results)
You can try with this (I'm using lodash 4.13.1). Thanks

Related

How can I access this property in my dictionary in js?

I thought I understood how to loop through a dictionary, but my loop is wrong. I try to access the name of each sub item but my code does not work.
Here is what I did:
list = [
{
title: 'Groceries',
items: [
{
id: 4,
title: 'Food',
cost: 540 ,
},
{
id: 5,
title: 'Hygiene',
cost: 235,
},
{
id: 6,
title: 'Other',
cost: 20,
},
],
}];
function calculateCost(){
let total = 0;
Object.keys(list).forEach((k) => { for (i in k.items) { total += i.data; } });
console.log(total);
return total;
}
Your list is an array includes 1 object and this object has two properties title and items the items here is an array of objects each one of these objects has property cost so to calculate the total cost you need to loop through items array, here is how you do it:
let list = [
{
title: 'Groceries',
items: [
{
id: 4,
title: 'Food',
cost: 540 ,
},
{
id: 5,
title: 'Hygiene',
cost: 235,
},
{
id: 6,
title: 'Other',
cost: 20,
},
],
}];
function calculateCost(){
let total = 0;
list[0].items.forEach(el => {
total += el.cost;
})
console.log(total)
return total;
}
calculateCost();
Your list is an Array, not an Object.
Instead of Object.keys() use Array.prototype.reduce:
const calculateCost = (arr) => arr.reduce((tot, ob) =>
ob.items.reduce((sum, item) => sum + item.cost, tot), 0);
const list = [
{
title: 'Groceries',
items: [
{id: 4, title: 'Food', cost: 10},
{id: 5, title: 'Hygiene', cost: 20},
{id: 6, title: 'Other', cost: 30}
]
}, {
title: 'Other',
items: [
{id: 8, title: 'Scuba gear', cost: 39}
],
}
];
console.log(calculateCost(list)); // 99
Expanding on #Roko's and #mmh4all's answers, the following code adds several verification statements to handle cases where a deeply nested property in your data is not what you expect it to be.
const calculateCost = (orders) => {
let listOfCosts = [];
// For each 'order' object in the 'orders' array,
// add the value of the 'cost' property of each item
// in the order to 'listOfCosts' array.
orders.forEach(order => {
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
if (!Array.isArray(order.items)) { return; }
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isNaN
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/parseFloat
const orderCostArr = order.items.map(item =>
isNaN(item.cost) ? 0 : parseFloat(item.cost, 10));
if (orderCostArr.length === 0) { return; }
// Concatenate 'orderCostArr' to the 'listOfCosts' array
//listOfCosts = listOfCosts.concat(orderCostArry);
// Alternate approach is to use the spread syntax (...) to
// push the items in the array returned by 'order.items.map()'
// into the 'listOfCosts' array.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax
listOfCosts.push(...orderCostArr);
});
// Use the 'reduce' method on the 'listOfCosts' array
// to get the total cost.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce
const totalCost = listOfCosts.reduce(
(accumulator, currentValue) => accumulator + currentValue, 0);
return totalCost;
};
const list = [
{
title: 'Groceries',
items: [
{ id: 4, title: 'Food', cost: 10 },
{ id: 3, title: 'Baked goods', cost: 20 },
{ id: 5, title: 'Hygiene', cost: 0 },
{ id: 6, title: 'Other' }
]
}, {
title: 'Gear',
items: {},
}, {
title: 'Accessories',
items: [],
}, {
title: 'Bags',
}, {
title: 'Other',
items: [
{ id: 10, title: 'Scuba gear', cost: "5" },
{ id: 8, title: 'Scuba gear', cost: "err" },
{ id: 9, title: 'Scuba gear', cost: 59 }
],
}
];
console.log(calculateCost(list)); // 94

Create an array of objects with values from another object

I have an object looking like this
const item = {
id: 123,
type: 'book',
sections: [{
type: 'section',
id: '456',
index: 1,
lessons: [{
type: 'lesson',
id: 789,
index: 1
},
{
type: 'lesson',
id: 999,
index: 2
}
]
}, {
type: 'section',
index: 2,
id: 321,
lessons: [{
type: 'lesson',
id: 444,
index: 1
},
{
type: 'lesson',
id: 555,
index: 2
}
]
}]
}
It should be assumed that there are more objects in sections and lessons array. I want to create a new object like this
result = [{
section: 456,
lessons: [789, 999]
}, {
section: 321,
lessons: [444, 555]
}]
I tried this loop but this just pushes indexes and not lesson's ids
let obj = {};
let sectionWithLessons = [];
let lessons = []
for (const i in item.sections) {
obj = {
sectionId: item.sections[i].id,
lessonIds: item.sections[i].lessons.map((lesson) => {
return lessons.push(lesson.id)
}),
};
sectionWithLessons.push(obj);
}
console.log(sectionWithLessons);
How can i do this correctly and preferably with good performance in consideration?
I believe the best/shortest thing is to use the map function, like:
const result2 = item.sections.map(({id, lessons}) => ({
id,
lessons: lessons.map(({id: lessionId}) => lessionId)
}))
I would suggest using Array.map() to convert the item sections to the desired result.
We'd convert each section into an object with a section value and lessons array.
To create the lessons array, we again use Array.map() to map each lesson to a lesson id.
const item = { id: 123, type: 'book', sections: [{ type: 'section', id: '456', index: 1, lessons: [{ type: 'lesson', id: 789, index: 1 }, { type: 'lesson', id: 999, index: 2 } ] }, { type: 'section', index: 2, id: 321, lessons: [{ type: 'lesson', id: 444, index: 1 }, { type: 'lesson', id: 555, index: 2 } ] }] }
const result = item.sections.map(({ id, lessons }) => {
return ({ section: +id, lessons: lessons.map(({ id }) => id) })
});
console.log('Result:', result);
.as-console-wrapper { max-height: 100% !important; }

Array of objects - how to group items by property and squash data from each of grouped items into 1

I am trying to perfom operation on array that is described in topic.
What i've done already - group by common property + map data of each item to desired format. It was achieved by using reduce + map function inside it.
Now i need final part that is going to squash each of grouped items properties into 1.
My goal is to make it work properly, ideally by extending restructuring function.
Array to restructure
const arrToRestructure = [
{
x: "test-x1",
y: "test-y1",
data: [
{id: 1, label: "label-y1-1"},
{id: 2, label: "label-y1-2"}
]
},
{
x: "test-x2",
y: "test-y1",
data: [
{id: 1, label: "label-y1-3"},
{id: 2, label: "label-y1-4"}
]
},
{
x: "test-x2",
y: "test-y2",
data: [
{id: 1, label: "label-y2-1"},
{id: 2, label: "label-y2-2"}
]
},
]
restructuring function
const restructureArr = () => {
const restructuredArr = arrToRestructure.reduce(
(prevVal, nextVal) => ({
...prevVal,
[nextVal["y"]]: [
...(prevVal[nextVal["y"]] || []),
nextVal.data.map((nextVal) => nextVal.label),
]})
,[]);
return restructuredArr;
}
current output
{
"test-y1": [
[
"label-y1-1",
"label-y1-2"
],
[
"label-y1-3",
"label-y1-4"
]
],
"test-y2": [
[
"label-y2-1",
"label-y2-2"
]
]
}
desired output
{
"test-y1": [
"label-y1-1",
"label-y1-2",
"label-y1-3",
"label-y1-4",
],
"test-y2": [
"label-y2-1",
"label-y2-2"
]
}
Simply spread the Array#map result each time:
const restructureArr = () => {
const restructuredArr = arrToRestructure.reduce((prevVal, nextVal) => ({
...prevVal,
[nextVal["y"]]: [
...(prevVal[nextVal["y"]] || []),
...nextVal.data.map((nextVal) => nextVal.label), // fix
]
}) , []);
return restructuredArr;
}
Some enhancements:
const restructureArr = (arrToRestructure = []) =>
arrToRestructure.reduce((acc, { y, data = [] }) => ({
...acc,
[y]: [
...(acc[y] || []),
...data.map(({ label }) => label),
]
}), []);
const arrToRestructure = [
{ x: "test-x1", y: "test-y1", data: [ {id: 1, label: "label-y1-1"}, {id: 2, label: "label-y1-2"} ] },
{ x: "test-x2", y: "test-y1", data: [ {id: 1, label: "label-y1-3"}, {id: 2, label: "label-y1-4"} ] },
{ x: "test-x2", y: "test-y2", data: [ {id: 1, label: "label-y2-1"}, {id: 2, label: "label-y2-2"} ] }
];
console.log(restructureArr(arrToRestructure));
You can group based on y value using array#reduce. Push all the label for same y value using array#forEach.
const arrToRestructure = [ { x: "test-x1", y: "test-y1", data: [ {id: 1, label: "label-y1-1"}, {id: 2, label: "label-y1-2"} ] }, { x: "test-x2", y: "test-y1", data: [ {id: 1, label: "label-y1-3"}, {id: 2, label: "label-y1-4"} ] }, { x: "test-x2", y: "test-y2", data: [ {id: 1, label: "label-y2-1"}, {id: 2, label: "label-y2-2"} ] }, ],
result = arrToRestructure.reduce((r, {y, data}) => {
r[y] ??= [];
data.forEach(({label}) => r[y].push(label));
return r;
},{});
console.log(result);
Another solution would be using Object.entries and .flat().
const obj = { "test-y1": [ [ "label-y1-1", "label-y1-2" ], [ "label-y1-3", "label-y1-4" ] ], "test-y2": [ [ "label-y2-1", "label-y2-2" ] ] }
const e = Object.entries(obj).map(el => ({[el[0]]: el[1].flat()}))
let o = Object.fromEntries(e.map(x => [Object.keys(x), Object.values(x)[0]]))
console.log(o);

Reduce not geting the right values [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 1 year ago.
Improve this question
I'm trying to make an array of arrays, where shoud be like:
{
data: [0, 0, 0],
name: ''
}
I made a simple code to test may real one and I noted that it is getting -1 on data where it should be other numbers.
So with the code above I should be able to do this:
0:
data: (12) [18093941, 18093941, 18093941, 18093941, 18093941, 18093941, 18093941, 18093941, 18093941, 18093941, 18093941, 35554793]
name: "Fuel"
What I am doing wrong?
const input = [
{
Data: '01/01/2009 00:00:00',
Fuel: '43575070.000',
Diesel: '5963754.000',
monthSingle: '01'
},
{
Data: '01/02/2009 00:00:00',
Fuel: 'NULL',
Diesel: '1509490.000',
monthSingle: '02'
},
{
Data: '01/03/2009 00:00:00',
Fuel: '21061010.000',
Diesel: '6887.000',
monthSingle: '03'
},
{
Data: '01/04/2009 00:00:00',
Fuel: '21061010.000',
Diesel: '6887.000',
monthSingle: '04'
}
]
const MesesOptions = [
{
id: 1,
value: '01',
label: 'Janeiro',
sufix: 'Jan'
},
{
id: 2,
value: '02',
label: 'Fevereiro',
sufix: 'Fev'
},
{
id: 3,
value: '03',
label: 'Março',
sufix: 'Mar'
}
]
const TheOutPut = input.reduce((acc, cur) => {
// console.log(acc, cur)
return [
{
name: 'Fuel',
data: MesesOptions.map((month) =>
parseFloat(acc.find((x) => x.name === 'Fuel').data.indexOf(month.id -1)) +
parseFloat(cur.monthSingle === month.value ? cur.Fuel ?? 0 : 0))
}
]
}, [
{ name: 'Fuel', data: [] }, { name: 'Aux', data: [0] }
]
)
console.log(TheOutPut)
I was not sure of what you wanted so I have provided two probable solutions based on an assumption of what I think you might be looking for.
const input = [{
Data: '01/01/2009 00:00:00',
Ilha: '1',
Fuel: '43575070.000',
Diesel: '5963754.000',
TermFuel: 'NULL',
Hidrica: '2084379.000',
Geotermica: '14775549.410',
Eolica: '3771650.000',
Biogas: '2502.000',
Aux: '1634838.600'
},
{
Data: '01/02/2009 00:00:00',
Ilha: '1',
Fuel: 'NULL',
Diesel: '1509490.000',
TermFuel: 'NULL',
Hidrica: 'NULL',
Geotermica: 'NULL',
Eolica: '296600.000',
Biogas: 'NULL',
Aux: '56909.000'
},
{
Data: '01/03/2009 00:00:00',
Ilha: '1',
Fuel: '21061010.000',
Diesel: '6887.000',
TermFuel: 'NULL',
Hidrica: '1534690.000',
Geotermica: '14775549.410',
Eolica: 'NULL',
Biogas: '2502.000',
Aux: '478623.000'
},
{
Data: '02/01/2009 00:00:00',
Ilha: '1',
Fuel: '21061010.000',
Diesel: '6887.000',
TermFuel: 'NULL',
Hidrica: '1534690.000',
Geotermica: '14775549.410',
Eolica: 'NULL',
Biogas: '2502.000',
Aux: '478623.000'
}
];
const MesesOptions = [{
id: 1,
value: '01',
label: 'Janeiro',
sufix: 'Jan'
},
{
id: 2,
value: '02',
label: 'Fevereiro',
sufix: 'Fev'
},
{
id: 3,
value: '03',
label: 'Março',
sufix: 'Mar'
}
];
const results = input.reduce((acc, cur) => {
const keyVals = Object.entries(cur);
keyVals.map(([k, v], i) => {
const nameData = acc.find(a => a.name == k);
if (nameData) {
nameData.data = MesesOptions.map((m) => 0);
}
});
return acc;
}, [{
name: 'Fuel',
data: []
}, {
name: 'Aux',
data: [0]
}]);
const results2 = input.reduce((acc, cur) => {
const keyVals = Object.entries(cur);
keyVals.map(([k, v], i) => {
const nameData = acc.find(a => a.name == k);
if (nameData && !Number.isNaN(Number(v))) {
nameData.data.push(parseFloat(v));
}
});
return acc;
}, [{
name: 'Fuel',
data: []
}, {
name: 'Aux',
data: [0]
}]);
console.log(results);
console.log(results2);

How to update 2D array with another 2D array matching on an id

I have two lists and would like to update one with the other on a matching index.
let cart = [{
header: some_data,
items: [{
id: 7,
properties: some_props_7,
time: 12345
}, {
id: 19,
properties: some_props_19,
time: 13344
}, {
id: 24,
properties: some_props_24,
time: 14342
}]
}, etc.];
let newData = [{
header: some_data,
items: [{
id: 19,
properties: some_new_props_19,
time: 17744
}, {
id: 24,
properties: some_new_props_24,
time: 18342
}]
}, etc.];
I am iterating over the cart, but am not coming up with an efficient way to update. I think I need another for loop in the "update cart" section, but that seems sub-optimal to me.
k is a list of indices
let i = 0, j = 0, l = cart.length;
for (i = 0; i < l; i++) {
let m = cart[i]['items'].length;
for (j = 0; j < m; j++) {
// update cart index with associated newData matching on id
}
}
How do I update the header and items data in the cart from a dynamic newData list?
Filter out overlaps and concatenate with new items:
let cart = {
header: 'some_data',
items: [{
id: 7,
properties: 'some_props_7',
time: 12345
}, {
id: 19,
properties: 'some_props_19',
time: 13344
}, {
id: 24,
properties: 'some_props_24',
time: 14342
}]
};
let newData = {
header: 'some_data',
items: [{
id: 19,
properties: 'some_new_props_19',
time: 17744
}, {
id: 24,
properties: 'some_new_props_24',
time: 18342
}]
};
//TEST
console.log(
cart.items.filter(item => !newData.items.map(i => i.id).includes(item.id)).concat(newData.items)
);
Updated in response to comments
It now maps properly over lists.
var cart = [{
header: 'some_data',
items: [{
id: 7,
properties: 'some_props_7',
time: 12345
}, {
id: 19,
properties: 'some_props_19',
time: 13344
}, {
id: 24,
properties: 'some_props_24',
time: 14342
}]
}];
var newData = [{
header: 'some_data',
items: [{
id: 19,
properties: 'some_new_props_19',
time: 17744
}, {
id: 24,
properties: 'some_new_props_24',
time: 18342
}]
}];
//TEST
cart = cart
//Modify existing data elements
.map(function(car) {
newData.forEach(function(data) {
if (data.header === car.header) {
car.items = car.items.filter(function(item) {
return !data.items.map(function(i) {
return i.id;
}).includes(item.id);
}).concat(data.items);
}
});
return car;
})
//Add new data elements
.concat(newData.filter(function(data) {
return !cart.some(function(car) {
return car.header === data.header;
});
}));
console.log(cart);

Categories

Resources