Efficient way to get object from array based on key [duplicate] - javascript

This question already has answers here:
Find a value in an array of objects in Javascript [duplicate]
(20 answers)
Get JavaScript object from array of objects by value of property [duplicate]
(17 answers)
Closed 4 years ago.
I have an array of objects as:
0: {id: "1", name: "Tab1", address: "123 Street"}
1: {id: "2", name: "Tab2", address: "456 Avenue"}
2: {id: "3", name: "Tab3", address: "789 st"}
I want to grab a particular object from the above array based on the "name" key. For example, if I pass the key as "Tab1" it should return me:
0: {id: "1", name: "Tab1", address: "123 Street"}
I can loop through the array and get the values but wanted to know is there any simpler/efficient way to get the desired data.

You can use Array.prototype.find like this
const searchArray = key => array.find(({name})=> {
return name === key;
});
console.log(searchArray("Tab1"));

Related

How do I push an element into the array within an object in AngularJS [duplicate]

This question already has answers here:
How do I add items to a nested Array?
(4 answers)
Closed last year.
I have the following JavaScript object :
[{
Name: "Ishan",
Code: 1,
Is_Intern: "Yes",
DateOfJoining: "01/02/2022",
Skills: ["VB, DOT.NET, Angular"],
Status: "Working"
}]
How can I add an item to the skills array in this object?
I didn't find any particular article regarding this.
var data = [{
Name: "Ishan",
Code: 1,
Is_Intern: "Yes",
DateOfJoining: "01/02/2022",
Skills: ["VB, DOT.NET, Angular"],
Status: "Working"
}]
data[0].Skills.push("New Skill");
console.log(data);

Is there any good way to get new Array from new Array when things in Array has common factors?(react.js) [duplicate]

This question already has answers here:
javascript filter array of objects
(8 answers)
Get JavaScript object from array of objects by value of property [duplicate]
(17 answers)
Closed 1 year ago.
Suppose there is an Array that has some objects like below
[{name:"Bruce wayne" age:42 DOA:true},{name: "Dick grayson" age:28 DOA:true},{name:"Jason todd" age:24 DOA:false}]
and I want to get a new Array with Objects which DOA is true (bruce and dick).
Is there any good API or something to do this?
your help is going to be appreciated Thx!
You can simply use Array.prototype.filter:
const data = [{
name: "Bruce wayne",
age: 42,
DOA: true
}, {
name: "Dick grayson",
age: 28,
DOA: true
}, {
name: "Jason todd",
age: 24,
DOA: false
}];
console.log(data.filter(obj => obj.DOA));
Mind that your JSON was also invalid.

Loop through json object to get values of particular key [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 2 years ago.
0: {id: "7B5B201E-35AA-48A1-B919-002445319F8B", name: "Naman Sabarwal"}
1: {id: "EA6672BA-4F7A-4214-A37F-00716CE698C9", name: "me name"}
2: {id: "01F29920-9206-42DF-8151-00A6A080C501", name: "Nitesh Negi"}
I want to get a list such that the list contains only the name key values.
listOfNames = ['sonu singh','me name','harman jain']
How to get all the values of the key 'name'?
listOfNames = jsonValues.map(x=>x.name)
You can try as follow.
let data = [
{id: "7B5B201E-35AA-48A1-B919-002445319F8B", name: "Naman Sabarwal"},
{id: "EA6672BA-4F7A-4214-A37F-00716CE698C9", name: "me name"},
{id: "01F29920-9206-42DF-8151-00A6A080C501", name: "Nitesh Negi"}
]; // assume the data is in array
let result = data.map( d => d.name );
console.log(result);
if it is json object like
details = [{id:"",name:""},{id:"",name:""},{id:"",name:""}]
you can use map function like
function getnames(){
let names = []
details.map((detail)=>{
names.push({name:detail.name})
return names
}
to shorten this
let names = values.map(value=>return value.name)

Find within array of objects the item having maximum value of certain property and return another property of that item [duplicate]

This question already has answers here:
From an array of objects, how to return property `b` of the object that has the highest property `a`?
(4 answers)
Finding the max value of an attribute in an array of objects
(21 answers)
Closed 2 years ago.
Trying to create a function to return the Name if the price is the highest using javascript. Still learning.
{
Name: "Effective Yoga Habits",
type: "book",
price: 10.99
},
{
Name: "Yoga kit",
type: "product",
price: 199.99
},
{
Name: "Medative surroundings",
type: "CD",
price: 6.00
}
]
If anyone can help I would appreciate it. Thank you in advance
You may use Array.prototype.reduce() to traverse your array and have accumulator to store the record with greatest price value:
const src = [{Name:"Effective Yoga Habits",type:"book",price:10.99},{Name:"Yoga kit",type:"product",price:199.99},{Name:"Medative surroundings",type:"CD",price:6.00}],
result = src
.reduce((r, {Name,price}) =>
price > r.price ? {Name,price} : r)
.Name
console.log(result)

Get value of a key from a dictionary array JavaScript [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 4 years ago.
so in Javascript I have an array structured like this:
car = [{id: "1", brand: "Opel"}, {id: "2", brand: "Haima"},{id: "3", brand: "Toyota"}]
How can I get the values of all the brands only.
This should help:
const car = [{id: "1", brand: "Opel"}, {id: "2", brand: "Haima"},{id: "3", brand: "Toyota"}];
const brands = car.map(({ brand }) => brand);
You can use array map to return all brands. Look at the following use of map.
car.map(o => o.brand)

Categories