Get elements from two arrays and update/replace them in another - javascript

I need to get the ID’s from arr1 that match names in arr2 and replace/add these name ID pairs into arr3.
arr1: {
[
id: [1234, 12345],
name: ‘one’,
},
{
id: [1270, 67812],
name: ‘two’,
},
{
id: [4970, 5170],
name: ’three’,
},
{
id: [02413, 02613],
name: ‘four’,
},
{
id: [8970],
name: ‘five’,
},
{
id: [7170, 6570, 6370],
name: ‘six’,
},
{
id: [8170, 22012, 4578, 33613],
name: ‘seven’,
},
{
id: [21812, 6170, 20412, 42812, 22012, 8170],
name: ‘eight’,
}]
arr2: [{
name: ‘one’,
}, {
name: ‘two’,
}, ]`
This would be result that needs to get pushed into a third array replacing the name elements with both names and ids.
arr3: [{
id: [1234, 12345],
name: ‘one’,
}, {
id: [1270, 67812],
name: ‘two’,
}

Related

How to get values of child objects in an array of objects in javascript

states = [{
name: telangana,
cities: [{
id: 1,
name: foo
}, {
id: 2,
name: joo
}, {
id: 3,
name: goo
}]
},
{
name: punjab,
cities: [{
id: 4,
name: tyu
}, {
id: 5,
name: ery
}, {
id: 6,
name: doo
}]
},
{
name: mumbai,
cities: [{
id: 7,
name: eee
}, {
id: 8,
name: qqq
}, {
id: 9,
name: www
}]
},
]
I want response like [foo, joo, goo, tyu, ery,doo, eee,qqq,www]
Can someone help me ?
Just write one line:
Learn more about reduce() and map()
const states = [{ name: "telangana", cities: [{ id: 1, name: "foo" }, { id: 2, name: "joo" }, { id: 3, name: "goo" }] }, { name: "punjab", cities: [{ id: 4, name: "tyu" }, { id: 5, name: "ery" }, { id: 6, name: "doo" }] }, { name: "mumbai", cities: [{ id: 7, name: "eee" }, { id: 8, name: "qqq" }, { id: 9, name: "www" }] }, ];
const result = states.reduce((acc, { cities }) => [...acc, ...cities.map(({ name }) => name)], []);
console.log(result);
const getNames = (data) => {
const nameArr = [];
data.forEach((ele) => {
ele.cities.forEach((ele2) => {
nameArr.push(ele2.name);
})
})
return nameArr;
}
getNames(states);
Try this please!
states = [{
name: "telangana",
cities: [{
id: 1,
name: "foo"
}, {
id: 2,
name: "joo"
}, {
id: 3,
name: "goo"
}]
},
{
name: "punjab",
cities: [{
id: 4,
name: "tyu"
}, {
id: 5,
name: "ery"
}, {
id: 6,
name: "doo"
}]
},
{
name: "mumbai",
cities: [{
id: 7,
name: "eee"
}, {
id: 8,
name: "qqq"
}, {
id: 9,
name: "www"
}]
},
]
const wantedArray = []
for(i=0; i < states.length; i++){
for(j=0; j < states[i].cities.length; j++){
wantedArray.push(states[i].cities[j].name)
}
}
console.log(wantedArray)
Just give it an empty array, then you loop through the states indexes, each index in states will have a cities array, then you just need to loop it again in that array to get each name of the cities. From then, you are using the push method that Javascript provides to push it to the empty array.
Here's how I'm doing it in JSFiddle, there will have a better way to do this, too.

How to check if array of objects that contains another array of object has property

Hi I have an array of objects which contains another array of objects.
I need to find an object in array which contains another object in it's array with certain propery
ID.
Let's say i need to find an object in casses array which contains a user with certain ID.
ID for user is unique.
casses = [
{
caseName: 'case 1',
date: '2021-05-4',
id: '123',
user: [{name: 'Vlad', id: '1'}, {name: 'Misha', id: '2'}]
},
{
caseName: 'case 2',
date: '2021-05-4',
id: '123',
user: [{name: 'Alina', id: '3'}, {name: 'Alex', id: '4'}]
},
{
caseName: 'case 3',
date: '2021-05-4',
id: '123',
user: []
},
]
I could use a nested loops and so on. But i wondering is it possible to do with one line ?
Something like this but one level deeper:
let val = casses(item => item.id === element.id); ​
Assume your case with ID set to "3"
Try below
const ID = "3";
const casses = [
{
caseName: "case 1",
date: "2021-05-4",
id: "123",
user: [
{ name: "Vlad", id: "1" },
{ name: "Misha", id: "2" }
]
},
{
caseName: "case 2",
date: "2021-05-4",
id: "123",
user: [
{ name: "Alina", id: "3" },
{ name: "Alex", id: "4" }
]
},
{
caseName: "case 3",
date: "2021-05-4",
id: "123",
user: []
}
];
casses.find(item => item.user.some(subItem => subItem.id === ID));

Assign new properties to an array of Objects

I have an array of Objects
const options = [
{ id: 1, name: "Back Pain" },
{ id: 2, name: "Body aches" },
{ id: 3, name: "Cold Sores" },
{ id: 4, name: "Cough" },
{ id: 5, name: "Constipation" },
];
I am trying to write a function that will assign new properties to the object.
The output I am looking for is:
const options = [
{ value: 1, label: "Back Pain" },
{ value: 2, label: "Body aches" },
{ value: 3, label: "Cold Sores" },
{ value: 4, label: "Cough" },
{ value: 5, label: "Constipation" },
];
I have tried to loop through the array using a for loop, but can not figure it out.
Thanks for the help:)
You can do it like this:
const data=[{ id: 1, name: "Back Pain" },
{ id: 2, name: "Body aches" },
{ id: 3, name: "Cold Sores" },
{ id: 4, name: "Cough" },
{ id: 5, name: "Constipation" },
];
var result = data.map(({id:value, name:label})=>({value, label}));
console.log(result);

How to remove duplicated item in array of array

I have an array like that:
[{
id: 1,
name: 'Proposal'
},
{
id: 2,
name: 'Contract',
children: [
{
name: 'Approval',
component: '/approval'
},
{
name: 'Cancellation',
component: '/cancellation'
}
]
}]
and another like that:
[{
id: 1,
name: 'Proposal'
},
{
id: 2,
name: 'Contract',
children: [
{
name: 'Approval',
component: '/approval'
}
]
},
{
id: 3,
name: 'Example'
}]
My question is how is the best way to filter arrays, and remove one if have duplicated or more, even in array of children. Or the best, is like arrays merge!

Sort an array object descending orders (max length first.)

By using this I can able to get the ascending order how can I get the reverse order max length first.?
{
category: [
{ name: "Cat1", elements : [
{ name: name, id: id } ]
},
{ name: "Cat2", elements : [
{ name: name, id: id },
{ name: name, id: id },
{ name: name, id: id } ]
},
{ name: "Cat3", elements : [
{ name: name, id: id },
{ name: name, id: id } ]
}
]
}
My expected result is:
{
category: [
{ name: "Cat1", elements : [
{ name: name, id: id },
{ name: name, id: id },
{ name: name, id: id } ]
},
{ name: "Cat2", elements : [
{ name: name, id: id },
{ name: name, id: id } ]
},
{ name: "Cat3", elements : [
{ name: name, id: id } ]
}
]
}
This is my sort:
var sorted_categories = original.category.sort(function (one, other) {
return one.elements.length - other.elements.length;
});
I want reverse result from this question:
How to sort an array of objects based on a the length of a nested array in javascript
array = array.reverse();
Just reverse the array?
Reverse the operands of the subtraction.

Categories