create new arraylist from existing arraylistt - javascript

I have a nested data from API. I need only some values and objects from that array list.
So, I'm trying to new simple array list but I couldn't handle with array in it.
I'll explain with sample:
const dummyData = [
{
orderDate: '20220501',
orderNumber: '12345',
id: '1',
name: 'Customer 1',
lines: [
{ id: 1, product: 'PRD001', qty: 1},
{ id: 2, product: 'PRD002', qty: 3},
]
},
{
orderDate: '20220501',
orderNumber: '12346',
id: '2',
name: 'Customer 2',
lines: [
{ id: 1, product: 'PRD001', qty: 1 },
{ id: 2, product: 'PRD002', qty: 2 },
{ id: 3, product: 'PRD003', qty: 7 },
{ id: 4, product: 'PRD004', qty: 1 },
]
},
{
orderDate: '20220502',
orderNumber: '12347',
id: '3',
name: 'Customer 3',
lines: [
{ id: 1, product: 'PRD001', qty: 1 },
{ id: 2, product: 'PRD002', qty: 1 },
{ id: 3, product: 'PRD003', qty: 3 },
]
},
]
const currentData = data.map(order => (
{
orderDate: order.orderDate,
orderNumber: order.orderNumber,
lines:
order["lines"].map(line => ({
product: line.product,
qty: line.qty,
}))
}
))
console.log("order", currentData)
The output is:
[{"lines": [[Object], [Object]], "orderDate": "20220501", "orderNumber": "12345"}, {"lines": [[Object], [Object], [Object], [Object]], "orderDate": "20220501", "orderNumber": "12346"}, {"lines": [[Object], [Object], [Object]], "orderDate": "20220502", "orderNumber": "12347"}]
But I'm trying to create a new array list like this:
[{
"lines": [
{ product: 'PRD001', qty: 1
},
{ product: 'PRD002', qty: 3
},
],
"orderDate": "20220501",
"orderNumber": "12345"
}]
What i'm trying to do:
[
{
orderDate: '20220501',
orderNumber: '12345',
lines: [
{ id: 1, product: 'PRD001', qty: 1},
{ id: 2, product: 'PRD002', qty: 3},
]
},
{
orderDate: '20220501',
orderNumber: '12346',
lines: [
{ product: 'PRD001', qty: 1 },
{ product: 'PRD002', qty: 2 },
{ product: 'PRD003', qty: 7 },
{ product: 'PRD004', qty: 1 },
]
},
{
orderDate: '20220502',
orderNumber: '12347',
lines: [
{ product: 'PRD001', qty: 1 },
{ product: 'PRD002', qty: 1 },
{ product: 'PRD003', qty: 3 },
]
},
]

Related

How to keep from each parent only children by id from object

I am trying to keep only children from each parent if it matches a child's id from a given list of parent/child ids.
var parentIds = ['1','2','3'];
var childrenIds = ['2','3','5'];
Parent with id '1' should only have children with id '2', parent with id '2' should only have children with id '3' and parent with id '3' should only have children with id '5'. So, parent id matches children id in same position from both arrays.
Example of initial object:
var object = [
{
name: "parent1",
id: 1,
option_values: [
{
name: "child1",
id: 1
},
{
name: "child2",
id: 2
},
{
name: "child8",
id: 8
}
]
},
{
name: "parent2",
id: 2,
option_values: [
{
name: "child3",
id: 3
},
{
name: "child1",
id: 1
},
{
name: "child9",
id: 9
}
]
},
{
name: "parent3",
id: 3,
option_values: [
{
name: "child5",
id: 5
},
{
name: "child4",
id: 4
},
{
name: "child13",
id: 13
}
]
},
]
Expected result:
var result = [
{
name: "parent1",
id: 1,
option_values: [
{
name: "child2",
id: 2
}
]
},
{
name: "parent2",
id: 2,
option_values: [
{
name: "child3",
id: 3
}
]
},
{
name: "parent3",
id: 3,
option_values: [
{
name: "child5",
id: 5
}
]
},
]
Loop through the objects. Find the index of the parent ID in the parentIds array, then get the corresponding element of childIds. Then filter the option_values array using that child ID.
var object = [{
name: "parent1",
id: 1,
option_values: [{
name: "child1",
id: 1
},
{
name: "child2",
id: 2
},
{
name: "child8",
id: 8
}
]
},
{
name: "parent2",
id: 2,
option_values: [{
name: "child3",
id: 3
},
{
name: "child1",
id: 1
},
{
name: "child9",
id: 9
}
]
},
{
name: "parent3",
id: 3,
option_values: [{
name: "child5",
id: 5
},
{
name: "child4",
id: 4
},
{
name: "child13",
id: 13
}
]
},
];
var parentIds = ['1', '2', '3'];
var childrenIds = ['2', '3', '5'];
object.forEach(obj => {
let idIndex = parentIds.indexOf(String(obj.id));
if (idIndex != -1) {
let childId = parseInt(childrenIds[idIndex]);
obj.option_values = obj.option_values.filter(child => child.id == childId);
}
});
console.log(object);

How to get list of parents id in json object

My nested json array looks like:
[
{
id: 1,
name: "Mike",
children: [
{ id: 2, name: "MikeC1" },
{ id: 3, name: "MikeC2" },
{
id: 4, name: "MikeC3",
children: [{ id: 5, name: "MikeCC1" }]
},
]
},
{
id: 6,
name: "Json",
children: [
{ id: 7, name: "JsonC1" },
{ id: 8, name: "JsonC2" },
{
id: 9, name: "JsonC3",
children: [{ id: 10, name: "JsonCC1" },{ id: 11, name: "JsonCC2" }]
},
]
}
]
Now I get a id like "11"
then get the parent ids array in json like [6,9,11]
How to do?
var id = 11
console.log(findParent(id))
//result is [6,9,11]
You need to do recursive search
const persons = [
{
id: 1,
name: "Mike",
children: [
{ id: 2, name: "MikeC1" },
{ id: 3, name: "MikeC2" },
{
id: 4, name: "MikeC3",
children: [{ id: 5, name: "MikeCC1" }]
},
]
},
{
id: 6,
name: "Json",
children: [
{ id: 7, name: "JsonC1" },
{ id: 8, name: "JsonC2" },
{
id: 9, name: "JsonC3",
children: [{ id: 10, name: "JsonCC1" },{ id: 11, name: "JsonCC2" }]
},
]
}
];
function searchRecursive(items, id) {
const allIds = [];
items.forEach(item => {
if(item.id === id) {
allIds.push(item.id);
}
else if(item.children) {
const ids = searchRecursive(item.children, id);
if(ids.length) allIds.push(item.id);
ids.forEach(id => allIds.push(id));
}
});
return allIds;
}
console.log(searchRecursive(persons, 11));

How to duplicate an object in an array by given quantity, ES6 and above

I'm trying to convert an array of objects where i return duplicated objects if the object properties quantity is greater than 1.
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
// desired return
[
{ id: 1, name: "Scissor", price: 2 }
{ id: 1, name: "Scissor", price: 2 }
{ id: 1, name: "Scissor", price: 2 }
{ id: 2, name: "Hat", price: 6.5}
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
{ id: 3, name: "Socks", price: 0.5 }
]
My code:
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
let newObjects= [];
Object.entries(objects).forEach(([key, value]) => {
for (let i=0; i < value.quantity; i++){
newObjects.push({ id: value.id, name: value.name, price: value.price})
}
});
console.log(newObjects);
So my code above does work, does return what i wanted, however i feel like there is a better/smoother and more of ES6 and beyond method. Could anyone please suggest a better way?
You could use .fill() and .flatMap().
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
let newObjects = objects.flatMap(e=>
Array(e.quantity).fill({id: e.id, name: e.name, price: e.price})
);
console.log(newObjects);
You can use an array reduce along with an array fill.
The map is required only if you want to have unique references otherwise you can fill using the same object.
const objects = [
{ id: 1, name: "Scissor", price: 2, quantity: 3 },
{ id: 2, name: "Hat", price: 6.5, quantity: 1 },
{ id: 3, name: "Socks", price: 0.5, quantity: 5 },
];
const output = objects.reduce((a, c) => {
return a.concat(Array(c.quantity).fill({}).map(x=>({
id: c.id,
name: c.name,
price: c.price
})))
}, []);
console.log(output)

Flatten array of objects having arrays nested within their properties

Currently I have an array that contains my clothing (fictitious data):
myClothes = [
{
type: 'shirts',
pieces: [
{
brand: 'patagonia',
quantity: 6,
},
{
brand: 'hugo boss',
quantity: 3,
},
],
},
{
type: 'trousers',
pieces: [
{
brand: 'jack & jones',
quantity: 2,
},
{
brand: 'zara',
quantity: 4,
},
{
brand: 'versace',
quantity: 1,
},
],
},
{
type: 'socks',
pieces: [
{
brand: 'no-name',
quantity: 12,
},
],
},
];
As you see, the array contains objects (shirts, trousers, socks), that seperate the clothing by their type. Now, what can I do with javascript to make a new array of that which looks like this below here?
So where just all pieces are unseperated from shirts/trousers/socks, and it should just show how much articles I have of the brand.
allPiecesUnseperated = [
{
brand: 'patagonia',
quantity: 6,
},
{
brand: 'hugo boss',
quantity: 3,
},
{
brand: 'jack & jones',
quantity: 2,
},
{
brand: 'zara',
quantity: 4,
},
{
brand: 'versace',
quantity: 1,
},
{
brand: 'no-name',
quantity: 12,
},
];
const clothes = [{
type: 'shirts',
pieces: [{
brand: 'patagonia',
quantity: 6,
},
{
brand: 'hugo boss',
quantity: 3,
},
],
},
{
type: 'trousers',
pieces: [{
brand: 'jack & jones',
quantity: 2,
},
{
brand: 'zara',
quantity: 4,
},
{
brand: 'versace',
quantity: 1,
},
],
},
{
type: 'socks',
pieces: [{
brand: 'no-name',
quantity: 12,
}, ],
},
];
const result = clothes.map(({
pieces
}) => pieces).reduce((acc, arr) => [...acc, ...arr], []);
console.log(result);
Use .flatMap() function
const myClothes = [
{
type: 'shirts',
pieces: [
{ brand: 'patagonia', quantity: 6 },
{ brand: 'hugo boss', quantity: 3 }
]
},
{
type: 'trousers',
pieces: [
{ brand: 'jack & jones', quantity: 2 },
{ brand: 'zara', quantity: 4, },
{ brand: 'versace', quantity: 1 }
]
},
{
type: 'socks',
pieces: [
{ brand: 'no-name', quantity: 12 },
]
}
];
const output = myClothes.flatMap(o => o.pieces);
console.log(output);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Probably not the most efficient (since concat does create a new array everytime it is invoked) or the most elegant solution, but should get the job done.
var all = []
myClothes.forEach((item) => {
all = all.concat(item.pieces)
})

How to unwind an Array of Objects in NodeJS?

I want to unwind an array of objects which have arrays of objects nested. The level of nesting is not defined and is not consistent throughout the array.
Here's my sample data
var data = [{
id: 1,
name: 'Harshal',
subjects: [{
id: 1,
name: 'English',
chapters: [{
id: 1,
name: 'Grammar'
}, {
id: 2,
name: 'Comprehension'
}]
}, {
id: 2,
name: 'Maths',
chapters: [{
id: 1,
name: 'Algebra'
}, {
id: 2,
name: 'Geometry'
}]
}]
}, {
id: 2,
name: 'Pankaj',
subjects: [{
id: 3,
name: 'Marathi',
chapters: [{
id: 1,
name: 'Kavita',
topics: [{
id: 1,
name: 'Topic 1'
}]
}]
}, {
id: 4,
name: 'Hindi',
chapters: [{
id: 1,
name: 'Katha',
topics: [{
id: 2,
name: 'Topic 2'
}, {
id: 3,
name: 'Topic 3'
}]
}]
}]
}];
I want to get an output like this:
var op = [{
id: 1,
name: 'Harshal',
subjects: {
id: 1,
name: 'English',
chapters: {
id: 1,
name: 'Grammar'
}
}
}, {
id: 1,
name: 'Harshal',
subjects: {
id: 1,
name: 'English',
chapters: {
id: 2,
name: 'Comprehension'
}
}
}, {
id: 1,
name: 'Harshal',
subjects: {
id: 2,
name: 'Maths',
chapters: {
id: 1,
name: 'Algebra'
}
}
}, {
id: 1,
name: 'Harshal',
subjects: {
id: 2,
name: 'Maths',
chapters: {
id: 2,
name: 'Geometry'
}
}
}, {
id: 2,
name: 'Pankaj',
subjects: {
id: 3,
name: 'Marathi',
chapters: {
id: 1,
name: 'Kavita',
topics: {
id: 1,
name: 'Topic 1'
}
}
}
}, {
id: 2,
name: 'Pankaj',
subjects: {
id: 4,
name: 'Hindi',
chapters: {
id: 1,
name: 'Katha',
topics: {
id: 2,
name: 'Topic 2'
}
}
}
}, {
id: 2,
name: 'Pankaj',
subjects: {
id: 4,
name: 'Hindi',
chapters: {
id: 1,
name: 'Katha',
topics: {
id: 3,
name: 'Topic 3'
}
}
}
}];
I have tried to work with pull-unwind but I guess there's some issues with it. If anyone has any other ideas, I'm open to implement those.
Have you tried recursion?
var data = [{
id: 1,
name: 'Harshal',
subjects: [{
id: 1,
name: 'English',
chapters: [{
id: 1,
name: 'Grammar'
}, {
id: 2,
name: 'Comprehension'
}]
}, {
id: 2,
name: 'Maths',
chapters: [{
id: 1,
name: 'Algebra'
}, {
id: 2,
name: 'Geometry'
}]
}]
}, {
id: 2,
name: 'Pankaj',
subjects: [{
id: 3,
name: 'Marathi',
chapters: [{
id: 1,
name: 'Kavita',
topics: [{
id: 1,
name: 'Topic 1'
}]
}]
}, {
id: 4,
name: 'Hindi',
chapters: [{
id: 1,
name: 'Katha',
topics: [{
id: 2,
name: 'Topic 2'
}, {
id: 3,
name: 'Topic 3'
}]
}]
}]
}];
function unravel(obj)
{
var out = [];
var added = false;
for(var i in obj) {
if(obj[i] instanceof Array) {
for(var j in obj[i]) {
var r = unravel(obj[i][j]);
for(var k in r) {
var a = {};
for(var key in obj) { // make copy of obj
a[key] = obj[key];
}
a[i] = r[k];
added = true;
out.push(a);
}
}
}
}
if(!added) {
out.push(obj);
}
return out;
}
var op = [];
for(var i in data)
op = op.concat(unravel(data[i]));
console.log(JSON.stringify(op, null, 4));

Categories

Resources