I have a little problem with this logic. I need to create a dynamic Array of Array. But the last object in every Array is not correct. I try some way but still not found expected result.
Here the snippet code :
const data = [{
"order-nr": 1,
"article": "Burger"
}, {
"order-nr": 1,
"article": "Soup"
}, {
"order-nr": 1,
"article": "Noodle"
}, {
"order-nr": 2,
"article": "Steak"
}, {
"order-nr": 2,
"article": "Chicken Wings"
}, {
"order-nr": 3,
"article": "Coffee"
}, {
"order-nr": 4,
"article": "Rice"
}]
let tempDataArticle = [];
let dataArticle = [];
let orderNr = 1;
let indexOrderNr = 0;
for (let i = 0; i < data.length; i++) {
const datarow = data[i];
dataArticle.push(datarow);
if (datarow['order-nr'] === orderNr) {
tempDataArticle[indexOrderNr] = dataArticle;
} else {
tempDataArticle[indexOrderNr] = dataArticle;
orderNr = orderNr + 1;
dataArticle = [];
indexOrderNr = indexOrderNr + 1;
}
}
console.log((tempDataArticle));
Output :
[
[{
"article": "Burger",
"order - nr": 1
}, {
"article": "Soup",
"order - nr": 1
}, {
"article": "Noodle",
"order - nr": 1
}, {
"article": "Steak",
"order - nr": 2
}],
[{
"article": "Chicken Wings",
"order - nr": 2
}, {
"article": "Coffee",
"order - nr": 3
}],
[{
"article": "Rice",
"order - nr": 4
}]
]
Here reproduce code.
Expected Result :
[
[{
"article": "Burger",
"order - nr": 1
}, {
"article": "Soup",
"order - nr": 1
}, {
"article": "Noodle",
"order - nr": 1
}],
[{
"article": "Steak",
"order - nr": 2
}, {
"article": "Chicken Wings",
"order - nr": 2
}],
[{
"article": "Coffee",
"order - nr": 3
}],
[{
"article": "Rice",
"order - nr": 4
}]
]
My expected result there is 4 Array base on order-nr key. So the total sub Array will depend on order-nr key.
3 Answers 3
const data = [{
"order-nr": 1,
"article": "Burger"
}, {
"order-nr": 1,
"article": "Soup"
}, {
"order-nr": 1,
"article": "Noodle"
}, {
"order-nr": 2,
"article": "Steak"
}, {
"order-nr": 2,
"article": "Chicken Wings"
}, {
"order-nr": 3,
"article": "Coffee"
}, {
"order-nr": 4,
"article": "Rice"
}]
let tempDataArticle = [];
for (let i = 0; i < data.length; i++) {
const order = data[i];
if(!tempDataArticle[order['order-nr'] - 1])
tempDataArticle[order['order-nr'] - 1] = [];
tempDataArticle[order['order-nr'] - 1].push(order)
}
console.log(tempDataArticle);
2 Comments
tempDataArticle[order['order-nr'] - 1].push(order) . Usualy I create temporary container to reuse case like this.Multiples problems here :
indexOrderNr = indexOrderNr + 1; was happening after you detected the item from another order, so the first item of an order would be added to the previous order.
You modified tempDataArticle[indexOrderNr] at every iteration, which is bad since you really don't need to and it makes the program do the same thing multiple times.
Since dataArticle.push(datarow); is done at the start of a new iteration, you add an item to an order without even checking from which order this item is from.
Friendly advice, you can save a lot of time by first working your logic on paper and then start to code.
Here is a working code snippet :
const data = [{
"order-nr": 1,
"article": "Burger"
}, {
"order-nr": 1,
"article": "Soup"
}, {
"order-nr": 1,
"article": "Noodle"
}, {
"order-nr": 2,
"article": "Steak"
}, {
"order-nr": 2,
"article": "Chicken Wings"
}, {
"order-nr": 3,
"article": "Coffee"
}, {
"order-nr": 4,
"article": "Rice"
}]
let tempDataArticle = [];
let dataArticle = [];
let orderNr = 1;
let indexOrderNr = 0;
for (let i = 0; i < data.length; i++) {
var datarow = data[i];
if (datarow['order-nr'] === orderNr) {
dataArticle.push(datarow);
} else {
tempDataArticle[indexOrderNr] = dataArticle;
orderNr = orderNr + 1;
dataArticle = [];
dataArticle.push(datarow);
indexOrderNr = indexOrderNr + 1;
}
}
tempDataArticle[indexOrderNr] = dataArticle;
console.log((tempDataArticle));
4 Comments
tempDataArticle on line tempDataArticle[indexOrderNr] = dataArticle;. Its outside on loop. But dataArticle will replace with new [] if condition doesn't meetorderNr. If Yes, add that article to dataArticle. If No, then we have all the articles of an order so it put them in tempDataArticle[indexOrderNr]. It then creates and another empty dataArticle, and adds the article from the new order. However, if this is the last order. The articles from the last order are not yet added since they are added only if it detects an article from a different order. So it needs to add the last order after the for loop is done.how about this solution:
const data = [{
"order-nr": 1,
"article": "Burger"
}, {
"order-nr": 1,
"article": "Soup"
}, {
"order-nr": 1,
"article": "Noodle"
}, {
"order-nr": 2,
"article": "Steak"
}, {
"order-nr": 2,
"article": "Chicken Wings"
}, {
"order-nr": 3,
"article": "Coffee"
}, {
"order-nr": 4,
"article": "Rice"
}]
let newArr = [];
data.forEach(item => {
if (newArr[item["order-nr"] - 1] === undefined) {
newArr[item["order-nr"] - 1] = []
}
newArr[item["order-nr"] - 1].push(item);
})
console.log(newArr);
order-nrkey.