Get value inside array and array of objects in JS [duplicate] - javascript

This question already has answers here:
JavaScript Recursive Search On An Array Of Objects
(4 answers)
JavaScript recursive search in JSON object
(8 answers)
Find an element in an array recursively
(2 answers)
Closed 6 days ago.
I have a similar array in the below format. I need to be able to find the object based on the id and it has to search for all the nested arrays (parent and child). And, the selectedId could be also be from the id of parent array.
Could anyone please advise?
Excerpt from my code
const cars = [{
id: 1,
name: 'toyota',
subs: [{
id: 43,
name: 'supra'
}, {
id: 44,
name: 'prius'
}]
}, {
id: 2,
name: 'Jeep',
subs: [{
id: 30,
name: 'wranger'
}, {
id: 31,
name: 'sahara'
}]
}]
const selectedId = 31;
const result = cars.find((val) => val.subs.id === selectedId)
console.log(result)
My expected output should be
{id: 31, name: 'sahara'}

Related

How to verify a key of object exists within a Javascript array of objects? [duplicate]

This question already has answers here:
How to determine if Javascript array contains an object with an attribute that equals a given value?
(27 answers)
Closed 3 months ago.
I have a array like below.
cont arr= [ { id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 2, username: 'ted' } ]
Can I know how to verify a 'username' is exist in the array object?
if you want to check if a key exist in all elements of the array you can do this
const arr= [ { id: 1, username: 'fred' }, { id: 2, username: 'bill' }, { id: 2, username: 'ted' } ]
const existsKey = (data, key) => data.every(d => d.hasOwnProperty(key))
console.log('username', existsKey(arr, 'username'))
console.log('anotherKey', existsKey(arr, 'anotherKey'))

JavaScript: Best way to check if duplicate value for a particular key exists in an array of objects [duplicate]

This question already has answers here:
How can I check if the array of objects have duplicate property values?
(15 answers)
Closed 2 months ago.
I have an array of objects, for example:
[
{
id: 1243,
name: "Cola",
isLocationAssign: true,
location: 8
},
{
id: 1243,
name: "Cola",
isLocationAssign: true,
qty: 30,
location: 8
}
]
In the above array of objects we have the same value for location. What would be the best way to find out if duplicate values for a particular key exist in an array of objects? I can't think past multiple loops
let array = [
{
id:1234,
name: "Cola",
isLocationAssign: true,
location: 8
},
{
id: 1234,
name: "Cola",
isLocationAssign: true,
qty: 30,
location: 8
},
{
id: 1235,
name: "Cola",
isLocationAssign: true,
qty: 30,
location: 8
},
{
id: 1235,
name: "Cola",
isLocationAssign: true,
qty: 30,
location: 7
}
]
let duplicatedIdsItems = array.filter(a => a.id === 1234);
console.log(duplicatedIdsItems)
var valueArr = duplicatedIdsItems.map(function(item){ return item.location });
var isDuplicate = valueArr.some(function(item, idx){
return valueArr.indexOf(item) != idx
});```

Order array of objects by property date [duplicate]

This question already has answers here:
How do I format a Microsoft JSON date?
(42 answers)
How to sort an object array by date property?
(25 answers)
Closed 7 months ago.
how can i order this array of objects:
var arr= [
{ name: 'Mario', date: '/Date(16487438090000)/' },
{ name: 'Mario', date: '/Date(16987438090000)/' },
{ name: 'Mario', date: '/Date(16887438090000)/' }
]
According to the 'date' property, how can I order from largest to smallest? example I want you to return me an array like this:
[
{ name: 'Mario', date: '/Date(16987438090000)/' },
{ name: 'Mario', date: '/Date(16887438090000)/' }
{ name: 'Mario', date: '/Date(16487438090000)/' },
]

How to filter out items in array of objects based on item in property array in JavaScript [duplicate]

This question already has answers here:
How to filter object array based on attributes?
(21 answers)
How do I check if an array includes a value in JavaScript?
(60 answers)
Closed 10 months ago.
What is the best way to return items in an array of object if they contain a certain property in a property which is an array?
I have the following array of objects:
const menus = [
{
id: 1,
title: 'English',
locations: ['MENU', 'MENU_EN']
},
{
id: 2,
title: 'Spanish',
locations: ['MENU', 'MENU_SP']
},
{
id: 3,
title: 'German',
locations: ['MENU', 'MENU_DE']
},
{
id: 4,
title: 'German and english',
locations: ['MENU', 'MENU_EN', 'MENU_DE']
},
]
As an example, I am looking for a way to return any items which have 'MENU_EN' in the locations property. So I need a function which will return the following:
const filteredMenus = [
{
id: 1,
title: 'English',
locations: ['MENU', 'MENU_EN']
},
{
id: 4,
title: 'German and english',
locations: ['MENU', 'MENU_EN', 'MENU_DE']
},
]
You can filter menus array using Array.prototype.filter() by checking if locations property includes the desired MENU_EN with Array.prototype.includes():
Code:
const menus = [{id: 1,title: 'English',locations: ['MENU', 'MENU_EN']},{id: 2,title: 'Spanish',locations: ['MENU', 'MENU_SP']},{id: 3,title: 'German',locations: ['MENU', 'MENU_DE']},{id: 4,title: 'German and english',locations: ['MENU', 'MENU_EN', 'MENU_DE']},]
const filteredMenus = menus.filter(({ locations }) => locations.includes('MENU_EN'))
console.log(filteredMenus)

What is the most effective way to filter this array? [duplicate]

This question already has answers here:
How to get the difference between two arrays of objects in JavaScript
(22 answers)
Closed 1 year ago.
I have two arrays. My goal is to get a third array with users that exist in usersUpdated but do not exist in oldUsers. I know i have to apply multiple filters, but I do not know how.
const oldUsers = [
{ name: 'Fede', id: 1 },
{ name: 'Marce', id: 2 },
];
const usersUpdated = [
{ name: 'Fede', id: 1 },
{ name: 'Marce', id: 2 },
{ name: 'Ale', id: 3 },
{ name: 'Julian', id: 4 },
];
const expectedValue = [
{ name: 'Ale', id: 3 },
{ name: 'Julian', id: 4 }
];
Convert arrays to objects, use names as keys & ids as values, delete keys of old obj from updated obj then convert the updated obj back to array.

Categories