Efficiently pull unknown data, separate, and store in specific format? - javascript

I've got data I need to sort through, filter, and store in a specific way. I'll explain by showing. Here is the data:
var pieData, cakeData, icecreamData;
var desserts = [
{
pies: [
{
name: "blueberry",
count: 3
},
{
name: "pumpkin",
count: 6
},
{
name: "apple",
count: 9
}
],
cakes: [
{
name: "chocolate",
count: 3
},
{
name: "foam",
count: 6
},
{
name: "wedding",
count: 9
}
],
icecream: [
{
name: "chocolate",
count: 3
},
{
name: "strawberry",
count: 6
},
{
name: "mint-chip",
count: 9
}
],
date: "2016-01-06T00:00:00"
},
{
pies: [
{
name: "blueberry",
count: 2
},
{
name: "pumpkin",
count: 4
},
{
name: "apple",
count: 6
}
],
cakes: [
{
name: "chocolate",
count: 2
},
{
name: "foam",
count: 4
},
{
name: "wedding",
count: 6
}
],
icecream: [
{
name: "chocolate",
count: 2
},
{
name: "strawberry",
count: 4
},
{
name: "mint-chip",
count: 6
}
],
date: "2016-01-07T00:00:00"
},
{
pies: [
{
name: "blueberry",
count: 4
},
{
name: "pumpkin",
count: 8
},
{
name: "apple",
count: 12
}
],
cakes: [
{
name: "chocolate",
count: 4
},
{
name: "foam",
count: 8
},
{
name: "wedding",
count: 12
}
],
icecream: [
{
name: "chocolate",
count: 4
},
{
name: "strawberry",
count: 8
},
{
name: "mint-chip",
count: 12
}
],
date: "2016-01-08T00:00:00"
}
];
So I've got my data. The data is basically what types of pies, cakes, and icereams there are which can vary in number, name, and count. Each object in desserts is a day, with the date as the last property. I'll go straight to what I want to get out of it and then explain further after that. Here is what I need to get out of it:
pieData = [
{
name: "blueberry",
dates: ["2016-01-06T00:00:00", "2016-01-07T00:00:00", "2016-01-08T00:00:00"],
counts: [3, 2, 4]
},
{
name: "pumpkin",
dates: ["2016-01-06T00:00:00", "2016-01-07T00:00:00", "2016-01-08T00:00:00"],
counts: [6, 4, 8]
},
{
name: "apple",
dates: ["2016-01-06T00:00:00", "2016-01-07T00:00:00", "2016-01-08T00:00:00"],
counts: [9, 6, 12]
}
];
cakeData = [
{
name: "chocolate",
dates: ["2016-01-06T00:00:00", "2016-01-07T00:00:00", "2016-01-08T00:00:00"],
counts: [3, 2, 4]
},
{
name: "foam",
dates: ["2016-01-06T00:00:00", "2016-01-07T00:00:00", "2016-01-08T00:00:00"],
counts: [6, 4, 8]
},
{
name: "wedding",
dates: ["2016-01-06T00:00:00", "2016-01-07T00:00:00", "2016-01-08T00:00:00"],
counts: [9, 6, 12]
}
];
icecreamData = [
{
name: "chocolate",
dates: ["2016-01-06T00:00:00", "2016-01-07T00:00:00", "2016-01-08T00:00:00"],
counts: [3, 2, 4]
},
{
name: "strawberry",
dates: ["2016-01-06T00:00:00", "2016-01-07T00:00:00", "2016-01-08T00:00:00"],
counts: [6, 4, 8]
},
{
name: "mint-chip",
dates: ["2016-01-06T00:00:00", "2016-01-07T00:00:00", "2016-01-08T00:00:00"],
counts: [9, 6, 12]
}
];
So I need to pass the desserts variable to a function and have it set the pieData, cakeData, and icecreamData variables, declared at the top of the original data, to the array of objects I've shown in the second bit of code.
A Few Things to Note:
In the output data, the values correspond to the days they were originally assigned in the original data. For example, pieData[0].dates[0] corresponds to pieData[0].counts[0] as it appears in the original data.
There could be infinite types of each dessert or none. Meaning there could be 10 different objects in the "pie" array in the original data or none. But there will always be pies, cakes, and icecream arrays, whether they are empty or have a thousand objects in them.
I don't know what types of each dessert will come through. It could be anything. So the pie could be named "pumpkin" or it could be named "battery acid".
My only solution I could think of was to do multiple loops and nested loops and just overall too much code. I know there has to be some efficient wizardry to get this done right.

I understand the drive to want "minified" code, but I think it's best to keep things readable. Despite your reservations, I think a few nested loops make sense in this case.
Consider the following way of getting the desired result:
var pieData, cakeData, icecreamData;
var desserts = [{pies:[{name:"blueberry",count:3},{name:"pumpkin",count:6},{name:"apple",count:9}],cakes:[{name:"chocolate",count:3},{name:"foam",count:6},{name:"wedding",count:9}],icecream:[{name:"chocolate",count:3},{name:"strawberry",count:6},{name:"mint-chip",count:9}],date:"2016-01-06T00:00:00"},{pies:[{name:"blueberry",count:2},{name:"pumpkin",count:4},{name:"apple",count:6}],cakes:[{name:"chocolate",count:2},{name:"foam",count:4},{name:"wedding",count:6}],icecream:[{name:"chocolate",count:2},{name:"strawberry",count:4},{name:"mint-chip",count:6}],date:"2016-01-07T00:00:00"},{pies:[{name:"blueberry",count:4},{name:"pumpkin",count:8},{name:"apple",count:12}],cakes:[{name:"chocolate",count:4},{name:"foam",count:8},{name:"wedding",count:12}],icecream:[{name:"chocolate",count:4},{name:"strawberry",count:8},{name:"mint-chip",count:12}],date:"2016-01-08T00:00:00"}];
var dessertData= {};
for (var dessertGroup of desserts) {
for (var item in dessertGroup) {
// the timestamp is not a dessert, so skip it
if (item === 'date') { continue; }
if (!dessertData[item]) {
// this is a new kind of dessert, add it
dessertData[item] = [];
}
for (var flavour of dessertGroup[item]) {
// get the index of the flavour
var index = dessertData[item].map(function(e) { return e.name; }).indexOf(flavour.name);
if (index < 0) {
// this is a new flavour of dessert, add it to the dessert type
dessertData[item].push({
name: flavour.name,
dates: [],
counts: []
});
index = dessertData[item].length - 1;
}
// append the relevant data to the flavour properties
dessertData[item][index].dates.push(dessertGroup.date);
dessertData[item][index].counts.push(flavour.count);
}
}
}
// finally, we want 'cakes' in 'cakeData'
// 'pies' in 'pieData'
// and 'icecream in 'icecreamData'
cakeData = dessertData.cakes;
pieData = dessertData.pies;
icecreamData = dessertData.icecream;
console.log("cakeData=", cakeData);
console.log("pieData=", pieData);
console.log("icecreamData=", icecreamData);
This is easy to read and modify. Also, it allows for any type of dessert! Why limit yourself to pies, cake, and icecream.
You'll notice that I'm dynamically creating and accessing the properties of dessertData by doing stuff like dessertData[propertyName].
Maybe you knew that was possible, but I used javascript for a long time before learning that the [] syntax wasn't just for numerical indices. Good luck!

This converts it into the format you want
https://jsfiddle.net/sdhjL7dv/
var pieData = [], cakeData = [], icecreamData = [];
var desserts = [
{
pies: [
{
name: "blueberry",
count: 3
},
{
name: "pumpkin",
count: 6
},
{
name: "apple",
count: 9
}
],
cakes: [
{
name: "chocolate",
count: 3
},
{
name: "foam",
count: 6
},
{
name: "wedding",
count: 9
}
],
icecream: [
{
name: "chocolate",
count: 3
},
{
name: "strawberry",
count: 6
},
{
name: "mint-chip",
count: 9
}
],
date: "2016-01-06T00:00:00"
},
{
pies: [
{
name: "blueberry",
count: 2
},
{
name: "pumpkin",
count: 4
},
{
name: "apple",
count: 6
}
],
cakes: [
{
name: "chocolate",
count: 2
},
{
name: "foam",
count: 4
},
{
name: "wedding",
count: 6
}
],
icecream: [
{
name: "chocolate",
count: 2
},
{
name: "strawberry",
count: 4
},
{
name: "mint-chip",
count: 6
}
],
date: "2016-01-07T00:00:00"
},
{
pies: [
{
name: "blueberry",
count: 4
},
{
name: "pumpkin",
count: 8
},
{
name: "apple",
count: 12
}
],
cakes: [
{
name: "chocolate",
count: 4
},
{
name: "foam",
count: 8
},
{
name: "wedding",
count: 12
}
],
icecream: [
{
name: "chocolate",
count: 4
},
{
name: "strawberry",
count: 8
},
{
name: "mint-chip",
count: 12
}
],
date: "2016-01-08T00:00:00"
}
];
for(var i = 0; i < desserts.length; i++) {
var d = desserts[i].date;
desserts[i].pies.length && save(pieData, desserts[i].pies, d);
desserts[i].cakes.length && save(cakeData, desserts[i].cakes, d);
desserts[i].icecream.length && save(icecreamData, desserts[i].icecream, d);
}
function save(destination, items, d) {
for(var i = 0; i < items.length; i++) {
var name = items[i].name;
var count = items[i].count;
if(destination[name] === undefined) { destination[name] = {name:'',dates:[],counts:[]}; }
destination[name].name = name;
destination[name].dates.push(d);
destination[name].counts.push(count);
}
}
console.log(pieData);
console.log(cakeData);
console.log(icecreamData);

Related

Combine arrays from two objects into a single object inline

I have two objects and some of their properties are identical and I need to combine these properties into a single array for another operation. Here are the objects:
const grippers = [
{
relevantRegisters: {
R: [
{ ID: 1 },
{ ID: 2 },
{ ID: 3 },
],
},
},
]
const pallets = [
{
relevantRegisters: {
R: [
{ ID: 1 },
{ ID: 2 },
{ ID: 3 },
],
},
},
]
Note that pallets and grippers are arrays, there can be more than one, so I cant just do pallets[0].relevantRegisters.R and take it from there. So it could be like this:
const grippers = [
{
relevantRegisters: {
R: [
{ ID: 1 },
{ ID: 2 },
{ ID: 3 },
],
},
},
{
relevantRegisters: {
R: [
{ ID: 1 },
{ ID: 2 },
{ ID: 3 },
],
},
},
{
relevantRegisters: {
R: [
{ ID: 1 },
{ ID: 2 },
{ ID: 3 },
],
},
},
]
I want to have a final array with the combined objects from the R: arrays, like this (not the values of the ID's, but the objects that contain the ID!):
[{ID: 1}, {ID: 2}, {ID: 3}, {ID: 1}, {ID: 2}, {ID: 3}]
Here is what I have tried:
const extractedR = [
...pallets
.map((pallet) => {
return pallet.relevantRegisters.R;
}),
...grippers
.map((gripper) => {
return gripper.relevantRegisters.R;
}),
]
However the result from this is an array of an array each containing the IDs. [Array(3), Array(3)]
Note: I don't need just the ID's, I need the object that contains the ID's as there are other properties within it that I also need, so I need to end up with an Array of 6 objects. Instead I end up with a 2x3 Array.
If I separate the two maps into variables (discovered it while trying to debug it) and spread the variables into array then it works, and so I've tried "double spreading" inline (don't know if that even works) like [...[...pallets(..),], ...[...grippers(..)]] but it also didnt work. I need to be able to do this inline.
You can use flatMap
const grippers = [
{
relevantRegisters: {
R: [
{ ID: 1 },
{ ID: 2 },
{ ID: 3 },
],
},
},
]
const pallets = [
{
relevantRegisters: {
R: [
{ ID: 1 },
{ ID: 2 },
{ ID: 3 },
],
},
},
]
const extractedR = [
...pallets
.flatMap((pallet:any) => {
return pallet.relevantRegisters.R;
}),
...grippers
.flatMap((gripper:any) => {
return gripper.relevantRegisters.R;
}),
]
console.log(extractedR)
Is this what you want?
const grippers = [
{
relevantRegisters: {
R: [
{ ID: 1 },
{ ID: 2 },
{ ID: 3 },
],
},
},
]
const pallets = [
{
relevantRegisters: {
R: [
{ ID: 1 },
{ ID: 2 },
{ ID: 3 },
],
},
},
]
var arrayInline = [].concat(grippers[0].relevantRegisters.R).concat(pallets[0].relevantRegisters.R);
console.log(arrayInline);
You can use concat function for this.
const grippers = [
{
relevantRegisters: {
R: [
{ ID: 1 },
{ ID: 2 },
{ ID: 3 },
],
},
},
]
const pallets = [
{
relevantRegisters: {
R: [
{ ID: 1 },
{ ID: 2 },
{ ID: 3 },
],
},
},
]
let newArray = [];
grippers.filter(e => newArray = newArray.concat(e.relevantRegisters.R))
pallets.filter(e => newArray = newArray.concat(e.relevantRegisters.R))
console.log(newArray);
You can use array reduce to get your result.
Working Code
const grippers = [{
relevantRegisters: {
R: [{
ID: 1
},
{
ID: 2
},
{
ID: 3
},
],
},
}, ]
const pallets = [{
relevantRegisters: {
R: [{
ID: 1
},
{
ID: 2
},
{
ID: 3
},
],
},
}, ]
console.log([...grippers.map(({
relevantRegisters: {
R
}
}) => R).reduce((arr, val) => [...arr, val]), ...pallets.map(({
relevantRegisters: {
R
}
}) => R).reduce((arr, val) => [...arr, val])])
const extracted = [...grippers[0].relevantRegisters.R, ...pallets[0].relevantRegisters.R]
with the new requirement you could do it like this
[].concat.apply([], [...grippers.map(x => x.relevantRegisters.R), ...pallets.map(x => x.relevantRegisters.R)]);

Javascript re-order array of object by value

How do I re-order array of object showing below by follow value. If follow value is not -1, move the item below to the item that has the id value same as follow value.
Here is the example.
let charObj = [
{ id: 8, name: 'Catelyn Stark', follow: -1 },
{ id: 7, name: 'Jaime Lannister', follow: 8 },
{ id: 3, name: 'Jon Snow', follow: -1 },
{ id: 4, name: 'Daenerys Targaryen', follow: 7 },
{ id: 5, name: 'Sansa Stark', follow: 4 }
];
Expected output will be;
let charObj = [
{ id: 8, name: 'Catelyn Stark', follow: -1 },
{ id: 7, name: 'Jaime Lannister', follow: 8 },
{ id: 4, name: 'Daenerys Targaryen', follow: 7 },
{ id: 5, name: 'Sansa Stark', follow: 4 },
{ id: 3, name: 'Jon Snow', follow: -1 }
];
Not sure if I can use sort(). What is the best way to re-order this object?
I think this will do what you're asking. I'm sure it could be made more efficient, but unless your list gets quite large that shouldn't make much practical difference. Also, this assumes any character will only have one follower. If that's not the rule, then the function will have to be adjusted.
let charObj = [
{ id: 8, name: "Catelyn Stark", follow: -1 },
{ id: 7, name: "Jaime Lannister", follow: 8 },
{ id: 3, name: "Jon Snow", follow: -1 },
{ id: 4, name: "Daenerys Targaryen", follow: 7 },
{ id: 5, name: "Sansa Stark", follow: 4 }
];
function sortChars(chars) {
let result = [];
let leaders = chars.filter(c => c.follow === -1);
for (let i = 0; i < leaders.length; i++) {
let current = leaders[i];
while (current) {
result.push(current);
let next = charObj.find(c => c.follow === current.id);
current = next;
}
}
return result;
}
console.log(sortChars(charObj));

Sort JavaScript Array having the array of Object

Can you please suggest me the best way to sort the below array by the priority based on the name of the section. I am more worried on the time complexity as my array in real consist of 100 000 records.
I am okay also to change the array structure if there any better way to store
[{
id: 'field1',
sections: [{
name: 'Top_Section',
priority: 3
},
{
name: 'Bottom_Section',
priority: 3
}
]
},
{
id: 'field2',
sections: [{
name: 'Top_Section',
priority: 2
},
{
name: 'Bottom_Section',
priority: 4
}
]
},
{
id: 'field3',
sections: [{
name: 'Top_Section',
priority: 1
},
{
name: 'Bottom_Section',
priority: 1
}
]
},
{
id: 'field4',
sections: [{
name: 'Top_Section',
priority: 4
},
{
name: 'Bottom_Section',
priority: 2
}
]
}
];
Like I wanted to sort priority based on the Top_Section so my expected output should be as below
as the field3 is having priority 1 and field2 is having priority 2 etc.
[
{
id: 'field3',
sections: [
{ name: 'Top_Section', priority: 1 },
{ name: 'Bottom_Section', priority: 1 }
]
},
{
id: 'field2',
sections: [
{ name: 'Top_Section', priority: 2 },
{ name: 'Bottom_Section', priority: 4 }
]
},
{
id: 'field1',
sections: [
{ name: 'Top_Section', priority: 3 },
{ name: 'Bottom_Section', priority: 3 }
]
},
{
id: 'field4',
sections: [
{ name: 'Top_Section', priority: 4 },
{ name: 'Bottom_Section', priority: 2 }
]
}
];
I'm assuming here that 'Top_Section' is always on the first position in the sections array.
I'm also assuming that there will be only two types of priority types: 'Top_Section' and 'Bottom_Section'
let list = [{
id: 'field1',
sections: [{
name: 'Top_Section',
priority: 3
},
{
name: 'Bottom_Section',
priority: 3
}
]
},
{
id: 'field2',
sections: [{
name: 'Top_Section',
priority: 2
},
{
name: 'Bottom_Section',
priority: 4
}
]
},
{
id: 'field3',
sections: [{
name: 'Top_Section',
priority: 1
},
{
name: 'Bottom_Section',
priority: 1
}
]
},
{
id: 'field4',
sections: [{
name: 'Top_Section',
priority: 4
},
{
name: 'Bottom_Section',
priority: 2
}
]
}
];
function sortBy(priorityName) {
let priorityPosition = (priorityName == 'Top_Section') ? 0 : 1;
return (a, b) => {
return a['sections'][priorityPosition].priority - b['sections'][priorityPosition].priority;
}
}
console.log( list.sort(sortBy('Top_Section')) );
Let's create a comparator
function compare(a, b) {
var sumA = 0;
var sumB = 0;
for (var section of a.sections) sumA += section.priority;
for (var section of b.sections) sumB += seciton.priority;
return sumB - sumA;
}
arr.sort(compare);
The comparator returns positive if the first parameter is greater, negative if the second parameter is greater and 0 if they are equal. I assumed that the lowest the numeric value of the sum of the priorities, the greater the item is.

How to avoid duplicate objects from two arrays after filter using java script

i have two arrays ( busns and pc ) i want to check pc array -> pcid is match with busns array -> pcid after match i want to return pcname. final output is comming but i want to avoid duplicate. thanks in advance
please check my attempt in jsfiddle
const busns = [
{
id:1,
shopname:'New trend',
pcid:1
},
{
id:2,
shopname:'Latest Beauty',
pcid:2
},
{
id:3,
shopname:'Mens Style',
pcid:1
},
{
id:4,
name:'Fabulook',
pcid: 1
},
{
id:4,
name:'New cut',
pcid: 2
}
]
const pc = [
{
pcid:1,
pcname: 'collection1'
},
{
pcid:2,
pcname: 'collection2'
},
{
pcid:3,
pcname: 'collection3'
},
{
pcid:4,
pcname: 'collection4'
}
]
My code :
busns.map(busns => {
return pc.filter( p => {
return busns.pcid == p.pcid
}).map(data => {
console.log(data.pcname)
})
})
expected output while using console :
collection1
collection2
As commented, you could use this:
var ids = busns.map(x=>x.pcid); pc.filter(x=> ids.includes(x.pcid))
Your code:
What you are doing is,
Looping on business array to get current PC id.
Then you use this ID to filter out from PC's list and then use map to get name.
Now the issue with your code is, since you are looping on busns array, you are essentially printing the name of PC for every id.
const busns = [{ id: 1, shopname: 'New trend', pcid: 1 }, { id: 2, shopname: 'Latest Beauty', pcid: 2 }, { id: 3, shopname: 'Mens Style', pcid: 1 }, { id: 4, name: 'Fabulook', pcid: 1 }, { id: 4, name: 'New cut', pcid: 2 } ]
const pc = [{ pcid: 1, pcname: 'collection1' }, { pcid: 2, pcname: 'collection2' }, { pcid: 3, pcname: 'collection3' }, { pcid: 4, pcname: 'collection4' } ]
busns.map(busns => {
return pc.filter(p => {
return busns.pcid == p.pcid
}).map(data => {
console.log(data.pcname)
})
})
Solution from comment:
Instead of nested looping structure, what you can do is,
Create a list of pcids. This will help you know if pc is traded.
Loop over pc array. Trades can be many but product will be finite and less in number. Use this to get a filtered list of products that are in busns.
Now loop over this filtered array and retrieve names. This part was not covered in comment.
const busns = [{ id: 1, shopname: 'New trend', pcid: 1 }, { id: 2, shopname: 'Latest Beauty', pcid: 2 }, { id: 3, shopname: 'Mens Style', pcid: 1 }, { id: 4, name: 'Fabulook', pcid: 1 }, { id: 4, name: 'New cut', pcid: 2 } ]
const pc = [{ pcid: 1, pcname: 'collection1' }, { pcid: 2, pcname: 'collection2' }, { pcid: 3, pcname: 'collection3' }, { pcid: 4, pcname: 'collection4' } ]
var ids = busns.map(x=>x.pcid);
var pcNames = pc
.filter(x=> ids.includes(x.pcid))
.map(x=> x.pcname);
console.log(pcNames)
Preferred solution:
One issue with above approaches is extra iterations and duplicate pcid in list. So instead of using functions like .map + .filter, we can use .reduce and create unique list. This will keep iterations minimum.
Create a hashMap with keys as pcid and value as true. This will create a unique id map.
Now loop over pc array and check if current id is present in this object ot not. If present, push name. If not, continue.
I would suggest you to use this approach as this should be more performant. Using .map + .filter + .indexOf can be expensive for huge arrays.
const busns = [{ id: 1, shopname: 'New trend', pcid: 1 }, { id: 2, shopname: 'Latest Beauty', pcid: 2 }, { id: 3, shopname: 'Mens Style', pcid: 1 }, { id: 4, name: 'Fabulook', pcid: 1 }, { id: 4, name: 'New cut', pcid: 2 } ]
const pc = [{ pcid: 1, pcname: 'collection1' }, { pcid: 2, pcname: 'collection2' }, { pcid: 3, pcname: 'collection3' }, { pcid: 4, pcname: 'collection4' } ]
var ids = busns.reduce((acc, cur) => acc[cur.pcid] = true && acc, {});
var pcNames = pc.reduce((acc, cur) => {
if(ids[cur.pcid]) {
acc.push(cur.pcname);
}
return acc;
}, [])
console.log(pcNames)
Something like this.
pc.filter(p => busns.map(busns => busns.pcid).includes(p.pcid)).forEach(data => console.log(data.pcname));

Compare and reduce complex array of objects

I have a ``dataset which is an array of objects for some items in a database that has the details of how long it will take in estimatedDays for a specific item to be shipped:
items : [
{
id: '1'
shippingMethods: [
{
id: 'STANDARD',
estimatedDays: 3,
},
{
id: 'TWODAY',
estimatedDays: 2,
},
{
id: 'NEXTDAY',
estimatedDays: 1,
},
]
},
{
id: '2'
// same shipping data as above but standard shipping will take 4 estimatedDays
},
{
id: '3'
// same shipping data as above but TWODAY shipping will take 3 estimatedDays
},
]
I am wondering if there is a reduce function that could compare each shippingMethod.id in each item and return a new array back only where shippingMethod.estimatedDays is greatest compared to all items.
So the end array would be an array of objects with (in this case) 3 shipping methods: STANDARD, TWODAY, and NEXTDAY.
Here you go with the reduce method,
reduce
var items = [
{
id: '1',
shippingMethods: [
{
id: 'STANDARD',
estimatedDays: 3
},
{
id: 'TWODAY',
estimatedDays: 2
},
{
id: 'NEXTDAY',
estimatedDays: 1
},
]
},
{
id: '2',
shippingMethods: [
{
id: 'STANDARD',
estimatedDays: 4
},
{
id: 'TWODAY',
estimatedDays: 2
},
{
id: 'NEXTDAY',
estimatedDays: 1
},
]
},
{
id: '3',
shippingMethods: [
{
id: 'STANDARD',
estimatedDays: 3
},
{
id: 'TWODAY',
estimatedDays: 3
},
{
id: 'NEXTDAY',
estimatedDays: 1
},
]
},
];
var outItems = items.reduce(function(accu, curr){
if(curr.shippingMethods) {
if(accu.length > 0) {
for(var i = 0; i < curr.shippingMethods.length; i++) {
var current = curr.shippingMethods[i];
if(accu[i].id === current.id && accu[i].estimatedDays < current.estimatedDays) {
accu[i] = current;
}
}
} else {
accu = curr.shippingMethods;
}
}
return accu;
}, []);
console.log(outItems);

Categories

Resources