Having a problem with two arrays of objects and I want to concat data of them related to their id's.
I have two arrays of objects like this:
const data = [
{
default: false,
id: 1,
value: true,
idModule: 1
},
{
default: false,
id: 2,
value: true,
idModule: 1
},
{
default: false,
id: 3,
value: true,
idModule: 2
},
{
default: false,
id: 4,
value: true,
idModule: 2
}
];
const modulData = [
{
name: 'Administration',
id: 1,
},
{
name: 'Benutzerverwaltung',
id: 2,
}
];
Now I want to combine these two related with their idModule == id
to create a new array of objects like this for example:
const result = [
{
name: 'Administration',
id: 1,
modul: [
{
default: false,
id: 1,
value: true,
idModule: 1
},
{
default: false,
id: 2,
value: true,
idModule: 1
},
],
},
{
name: 'Benutzerverwaltung',
id: 2,
modul: [
{
default: false,
id: 3,
value: false,
idModule: 2
},
{
default: false,
id: 4,
value: false,
idModule: 2
},
],
}
];
How can I achieve this?
You can do it simple by using map and filter:
let result = modulData.map(x => {
x.modul = data.filter(d => d.idModule === x.id);
return x;
});
Full code:
const data = [{
default: false,
id: 1,
value: true,
idModule: 1
},
{
default: false,
id: 2,
value: true,
idModule: 1
},
{
default: false,
id: 3,
value: true,
idModule: 2
},
{
default: false,
id: 4,
value: true,
idModule: 2
}
];
const modulData = [{
name: 'Administration',
id: 1,
},
{
name: 'Benutzerverwaltung',
id: 2,
}
];
let result = modulData.map(x => {
x.modul = data.filter(d => d.idModule === x.id);
return x;
});
console.log(result);
Related
When I click the button, I want to include all the objects in the itemSold and itemGet objects of the customers into the products array. how can I do that?
let customers = [{
active: true,
id: 1,
product: {
itemSold: [{id:1,name : 'car'}, {id:2,name : 'home'}],
itemGet: [{id:3,name : 'phone'}, {id:4,name : 'fly'}],
},
},
{
active: true,
id: 2,
product: {
itemSold: [{id:5,name : 'lamb'}, {id:6,name : 'mouse'}],
itemGet: [{id:7,name : 'mouse pad'}, {id:8,name : 'tv'}],
},
},
];
let clickButton = document.querySelector("#clickButton");
let products = [];
clickButton.addEventListener("click", getProcuts()});
function getProducts(){}
<button id="clickButton" >Click
</button>
let customers = [{
active: true,
id: 1,
product: {
itemSold: [{ id: 1, name: 'car' }, { id: 2, name: 'home' }],
itemGet: [{ id: 3, name: 'phone' }, { id: 4, name: 'fly' }],
},
},
{
active: true,
id: 2,
product: {
itemSold: [{ id: 5, name: 'lamb' }, { id: 6, name: 'mouse' }],
itemGet: [{ id: 7, name: 'mouse pad' }, { id: 8, name: 'tv' }],
},
},
];
let clickButton = document.querySelector("#clickButton");
let products = [];
clickButton.addEventListener("click", getProducts);
function getProducts() {
for (let i = 0; i < customers.length; i++) {
products.push(...customers[i].product.itemGet, ...customers[i].product.itemSold);
}
console.log(products);
}
<button id="clickButton">Click</button>
We loop our customers array and then select product property there we push both itemSold and itemGet arrays into products.
You can map over the customers and concatenate the arrays.
const customers = [
{
active: true,
id: 1,
product: {
itemSold: [
{ id: 1, name: "car" },
{ id: 2, name: "home" },
],
itemGet: [
{ id: 3, name: "phone" },
{ id: 4, name: "fly" },
],
},
},
{
active: true,
id: 2,
product: {
itemSold: [
{ id: 5, name: "lamb" },
{ id: 6, name: "mouse" },
],
itemGet: [
{ id: 7, name: "mouse pad" },
{ id: 8, name: "tv" },
],
},
},
];
const products = customers.map((customer) => {
return customer.product.itemSold.concat(customer.product.itemGet);
});
console.log(products);
const customers = [
{
active: true,
id: 1,
product: {
itemSold: [
{ id: 1, name: "car" },
{ id: 2, name: "home" },
],
itemGet: [
{ id: 3, name: "phone" },
{ id: 4, name: "fly" },
],
},
},
{
active: true,
id: 2,
product: {
itemSold: [
{ id: 5, name: "lamb" },
{ id: 6, name: "mouse" },
],
itemGet: [
{ id: 7, name: "mouse pad" },
{ id: 8, name: "tv" },
],
},
},
];
const products = customers.map((customer) => {
return customer.product.itemSold.concat(customer.product.itemGet).flat();
});
console.log(products.flat());
This question already has answers here:
How to map through a deeply nested array of objects?
(3 answers)
Closed 3 months ago.
I'm trying to extract those objects with the value required = true. I can make a forEach and push into a new array, but I would like to know how to do this with another method.
const arr = [
{ customFields: [{ id: 0, required: true }, { id: 1, required: false }] },
{ customFields: [{ id: 2, required: false }, { id: 3, required: false }] },
{ customFields: [{ id: 4, required: false }, { id: 5, required: true }] },
]
const requireds = arr.map(x => x.customFields.filter(y => y.required ))
console.log(requireds)
// expected = [{ id: 0, required: true }, { id: 5, required: true } ]
Flatten it first so you have a plain array of objects before filtering.
const arr = [
{ customFields: [{ id: 0, required: true }, { id: 1, required: false }] },
{ customFields: [{ id: 2, required: false }, { id: 3, required: false }] },
{ customFields: [{ id: 4, required: false }, { id: 5, required: true }] },
]
const requireds = arr
.flatMap(obj => obj.customFields)
.filter(y => y.required);
console.log(requireds)
Simyly use a .flatMap() to collect the results of your .filter() operation:
const arr = [
{ customFields: [{ id: 0, required: true }, { id: 1, required: false }] },
{ customFields: [{ id: 2, required: false }, { id: 3, required: false }] },
{ customFields: [{ id: 4, required: false }, { id: 5, required: true }] },
]
const requireds = arr.flatMap(x => x.customFields.filter(y => y.required ))
console.log(requireds)
// expected = [{ id: 0, required: true }, { id: 5, required: true } ]
I have this array :
defaultColumnsWithItems = [
{
column: 'Contrie', items: [
{ id: 1, label: 'USA', selectedItem: true },
{ id: 2, label: 'FRANCE', selectedItem: false },
{ id: 2, label: 'MAROC', selectedItem: false }
]
},
{
column: 'Categorie', items:
[
{ id: 0, label: 'Alimentaion', selectedItem: true },
{ id: 1, label: 'ricolage', selectedItem: false },
{ id: 2, label: 'Literie', selectedItem: true },
{ id: 3, label: 'Menage', selectedItem: false },
]
}
];
I want to filter only the elements with selected item withe value equal to true.
I try this:
const columnsWithSelectedItems = colmunsWithItemsToFilter.filter((columnWithItems) => {
return columnWithItems.items.filter( item => item.selectedItem === true)
})
but it return all elements.
Thank you.
Instead of filter the defaultColumnsWithItems, you should return the whole object with the condition for the items array.
In this scenario, we use the map function to return our object and for items array, we use the filter function to filter selectedItem in each item.
const defaultColumnsWithItems = [
{
column: 'Contrie', items: [
{ id: 1, label: 'USA', selectedItem: true },
{ id: 2, label: 'FRANCE', selectedItem: false },
{ id: 2, label: 'MAROC', selectedItem: false }
]
},
{
column: 'Categorie', items:
[
{ id: 0, label: 'Alimentaion', selectedItem: true },
{ id: 1, label: 'ricolage', selectedItem: false },
{ id: 2, label: 'Literie', selectedItem: true },
{ id: 3, label: 'Menage', selectedItem: false },
]
}
]
const items = defaultColumnsWithItems.map(el => {
return {
column: el.column,
items: el.items.filter(i => i.selectedItem)
}
})
console.log('items: ', items);
Try this
defaultColumnsWithItems.map(function(ComparisonResult) {
ComparisonResult.items = ComparisonResult.items.filter(x => x.selectedItem);
return ComparisonResult;
});
so I am trying to set up a nested filter on an array of objects.
The thing is that the filter is applied inside the object on a key that is another array of objects.
here is the code:
const items = [
{ name: "123", id: 1, value: true, arr: [{ id: 1 }] },
{ name: "456", id: 2, value: false, arr: [{ id: 2 }] },
{ name: "456", id: 2, value: false, arr: [{ id: 3 }] },
{ name: "456", id: 2, value: false, arr: [{ id: 4 }] },
{ name: "456", id: 2, value: false, arr: [{ id: 5 }] },
{ name: "456", id: 2, value: false, arr: [{ id: 6 }] },
];
const newArray = items.filter((objects) => {
objects.arr.filter((item) => {
if (item.id === 2) {
return objects;
}
});
});
console.log(newArray);
I 'm not sure where to put the return because in this situation i just get an empty array.
You need to check the nested array contains the wanted id and return the result to the filter method.
const
items = [{ name: "123", id: 1, value: true, arr: [{ id: 1 }] }, { name: "456", id: 2, value: false, arr: [{ id: 2 }] }, { name: "456", id: 2, value: false, arr: [{ id: 3 }] }, { name: "456", id: 2, value: false, arr: [{ id: 4 }] }, { name: "456", id: 2, value: false, arr: [{ id: 5 }] }, { name: "456", id: 2, value: false, arr: [{ id: 6 }] }],
result = items.filter(({ arr }) => arr.some(({ id }) => id === 2));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Use Array#some to check if current arr has an element with id equal to 2:
const items = [ { name: "123", id: 1, value: true, arr: [{ id: 1 }] }, { name: "456", id: 2, value: false, arr: [{ id: 2 }] }, { name: "456", id: 2, value: false, arr: [{ id: 3 }] }, { name: "456", id: 2, value: false, arr: [{ id: 4 }] }, { name: "456", id: 2, value: false, arr: [{ id: 5 }] }, { name: "456", id: 2, value: false, arr: [{ id: 6 }] } ];
const newArray = items.filter(({ arr = [] }) =>
arr.some(({ id }) => id === 2)
);
console.log(newArray);
As you contains only one object in arr then you can access this object by using [0] index.
Working Demo :
const items = [
{ name: "123", id: 1, value: true, arr: [{ id: 1 }] },
{ name: "456", id: 2, value: false, arr: [{ id: 2 }] },
{ name: "456", id: 2, value: false, arr: [{ id: 3 }] },
{ name: "456", id: 2, value: false, arr: [{ id: 4 }] },
{ name: "456", id: 2, value: false, arr: [{ id: 5 }] },
{ name: "456", id: 2, value: false, arr: [{ id: 6 }] },
];
const newArray = items.filter((obj) => {
if (obj.arr[0].id === 2) {
return obj;
}
});
console.log(newArray);
I answered you as per the issue you are facing but if you have multiple objects in your arr then you can go ahead with Array.some() method as suggested by other answers.
I have JS object like this one:
const networkTree = [
{
id: 10,
hasChildren: true,
children: [
{
id: 9,
hasChildren: true,
children: [
{
id: 7,
hasChildren: true,
children: [
{
id: 5,
hasChildren: false,
children: [],
},
{
id: 4,
hasChildren: false,
children: [],
},
],
},
{
id: 6,
hasChildren: true,
children: [
{
id: 3,
hasChildren: false,
children: [],
},
{
id: 2,
hasChildren: false,
children: [],
},
],
},
],
},
{
id: 8,
hasChildren: true,
children: [
{
id: 1,
hasChildren: false,
children: [],
},
{
id: 11,
hasChildren: false,
children: [],
},
],
},
],
},
];
I have to convert it with a function takes input js object like above to this order:
const myTreeData = [
{
name: 'Top Level',
children: [
{
name: 'Level 2: A',
},
{
name: 'Level 2: B',
},
],
},
];
I am stucked at this point:
const convertTree = (array) => (
treeDatas[];
treeData[];
for(i = 0 ; i < array.length ; i++) {
treeDatas.push(array[i]);
treeData[i].name.push(array[i].id);
}
);
If input networkTree[0].id is 15, myTreeData[0].name should be 15. I want to use id's as names on converted object. I don't need hasChildren boolean value. I will use converted object format with react-ds3-component. If you need more information about converted object format please check https://www.npmjs.com/package/react-d3-tree
If I understand you correctly you want to rename id to name and remove hasChildren? Then you can use this recursive function.
const convertTree = (tree) =>
tree.map((node) => ({
name: node.id,
children: convertTree(node.children)
}))
It's the same as this function, just less verbal.
const convertTree = (tree) => {
return tree.map((node) => {
return {
name: node.id,
children: convertTree(node.children)
}
})
}