array of object javascript code example
Example 1: sort by object property javascript
let list = [
{
name: "world"
},
{
name: "hello",
},
];
let x = list.sort((a, b) => (a.name > b.name ? 1 : -1));
console.log(x);
Example 2: list javascript
var fruits = ["Apple", "Banana", "Cherry"];
var books = [];
console.log(fruits[0])
fruits.push("Orange")
fruits[1]="Blueberry"
fruits.splice(0, 1)
fruits.splice(0, 2)
Example 3: object to array javascript
Object.values(obj)
Example 4: javascript sort array of object by property
function sortByDate( a, b ) {
if ( a.created_at < b.created_at ){
return -1;
}
if ( a.created_at > b.created_at ){
return 1;
}
return 0;
}
myDates.sort(sortByDate);
Example 5: array of objects javascript
var widgetTemplats = [
{
name: 'compass',
LocX: 35,
LocY: 312
},
{
name: 'another',
LocX: 52,
LocY: 32
}
]
Example 6: javascript array read object value in array
var events = [
{
userId: 1,
place: "Wormholes Allow Information to Escape Black Holes",
name: "Check out this recent discovery about workholes",
date: "2020-06-26T17:58:57.776Z",
id: 1
},
{
userId: 1,
place: "Wormholes Allow Information to Escape Black Holes",
name: "Check out this recent discovery about workholes",
date: "2020-06-26T17:58:57.776Z",
id: 2
},
{
userId: 1,
place: "Wormholes Allow Information to Escape Black Holes",
name: "Check out this recent discovery about workholes",
date: "2020-06-26T17:58:57.776Z",
id: 3
}
];
console.log(events[0].place);