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)
Related
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.
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)
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)
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"));
This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
Closed 6 years ago.
I want to delete object from my array posted below but nothing works. If I delete the properties it just goes to undefined. I have a recursive function that returns to me the right object. I wish to delete the second object and his children.
var data = {
parameters: [
{
id: 0,
name: "First",
value: "1",
children: []
},
{
id: 1,
name: "Second",
value: "2",
children: [
{
id: 2,
name: "Third",
value: "3",
children: []
}
]
}
],
index: 3
}
You could use Array#splice.
The splice() method changes the content of an array by removing existing elements and/or adding new elements.
data.parameters.splice(1, 1)
// index // \\ length