This question already has answers here:
Get JavaScript object from array of objects by value of property [duplicate]
(17 answers)
Find object by id in an array of JavaScript objects
(36 answers)
Closed 5 years ago.
Let's say I have an array of objects
const arr = [ {name:"Bob", age: 20}, { name: "Sara", age: 22}, { name:
Tom, age:20} ];
I want to print objects with particular property, for example only those with the age == 20. So The result would be
const arr = [ {name:"Bob", age: 20}, { name: Tom, age:20} ];
I really want to do it with ES6. Do you have any suggestion what method could be used?
This will do
var filteredData = arr.filter((e) => e.age === 20)
Related
This question already has answers here:
Sorting an array of objects by property values
(35 answers)
Sorting arrays numerically by object property value
(10 answers)
Closed 21 days ago.
var object = [
{ name: 'Mar', age: 32 ,height:40},
{ name: 'Carla', age: 10 ,height:15},
{ name: 'Hazel', age: 5 ,height:80}
];
Is it possible to sort based on the age element and generate a new sorted object?
Expected result:
var newobject = [
{ name: 'Hazel', age: 5 ,height:80},
{ name: 'Carla', age: 10 ,height:15},
{ name: 'Mar', age: 32 ,height:40}
];
Or you could also return the result sorted in an array, instead of returning it in an object... whatever... uncomplicated, but return it sorted .
var new_sorted_array = [
[Hazel, 5 ,80] ,
[Carla, 10 ,15],
[Mar, 32 ,40]
];
To achieve expected result, you can try below two options
Using array sort method - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
Second option is by looping array and using Object.values to get output in the format specified in question
var object = [
{ name: 'Mar', age: 32 ,height:40},
{ name: 'Carla', age: 10 ,height:15},
{ name: 'Hazel', age: 5 ,height:80}
];
//Sort array by property age
console.log("Sort by age", object.sort((a, b) => a.age - b.age))
//Using object values
console.log("Sort by age -option2", object.map(v => Object.values(v)).sort((a, b) => a[1] - b[1]))
This question already has answers here:
Sort array by firstname (alphabetically) in JavaScript [duplicate]
(23 answers)
Closed 1 year ago.
I am trying to sort an array by names in alphabetic order, with numbers it works, but with names doesn't. Why?
var arr = [{
name: 'Thomas',
age: 19
},
{
name: 'NoƩ',
age: 17
},
{
name: 'Andrey',
age: 27
},
{
name: 'Luc',
age: 20
}
]
const res = arr.sort((e1, e2) => e1.name - e2.name)
console.log(res)
Strings may not be subtracted, but you can compare them using localeCompare
const res = arr.sort((e1, e2) =>
e1.name.toLowerCase().localeCompare(e2.name.toLowerCase())
);
This question already has answers here:
Convert array of strings into an array of objects
(6 answers)
Closed 2 years ago.
I have an array of strings: const names = ['name1', 'name2', name3']
And I need the following array of objects:
const newArray = [{ name: 'name1' }, { name: 'name2' }, { name: 'name3' }]
How can I create newArray from names, or even possibly convert names itself without creating brand new array?
This can be easily achieved with the map function.
const newArray = names.map((name) => {
return {
name
};
});
map() can do that for you:
const names = ['name1', 'name2', 'name3'];
const newArray = names.map(x=>{return{name:x};});
console.log(newArray);
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 3 years ago.
How can i create another array by filtering one json array and include just the values pairs of a specific key?
Example (Filtering Value Pairs of Number Keys):
Array:
{ Name: 'abcd', Number: '1234' },
{ Name: 'efgh', Number: '5678' }
]````
Result Array:
````var filteredarray = ['1234','5678'];````
Thanks!
const a = [
{ Name: 'abcd', Number: '1234' },
{ Name: 'efgh', Number: '5678' }
]
function getNumbers(){
return a.map(item => item.Number);
}
getNumbers();
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 6 years ago.
I have an array of objects:
[{
name: "test",
age: 20,
gender: "male"
},
{
name: "test2",
age: 22,
gender: "female"
}]
Frequently I need to create a singleton array which contains a specific property from the object array above, for example extract only the names from the array above and create an array from it:
NewArray = ["test","test2"]
Currently I loop over the object array and push the property I need to the new array.
Is there a quick way to do it in Javascript/ES instead of looping every time I need to get specific property?
var people = [{
name:'test1',
age:20
}, {
name:'test2',
age:30
}]
let names = people.map(function(item) {
return item.name
});
console.log(names);