Order an array of objects by whether property exists [duplicate] - javascript

This question already has answers here:
Sorting an array of objects by property values
(35 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
I have an array of teachers, where each one have a property user with image property, that can contain a string or null value.
How can i order the array, considering the user.image property?
Here is an example of structure.
const teachers = [
{
id: 1,
user: {
id: 10,
image: "image.jgp"
}
},
{
id: 3,
user: {
id: 30,
image: null
}
},
{
id: 2,
user: {
id: 20,
image: "image.jgp"
}
},
{
id: 4,
user: {
id: 40,
image: null
}
},
]

You can use Array.sort and check whether the image property is null:
const teachers=[{id:1,user:{id:10,image:"image.jgp"}},{id:3,user:{id:30,image:null}},{id:2,user:{id:20,image:"image.jgp"}},{id:4,user:{id:40,image:null}}];
const sorted = teachers.sort((a, b) => b.user.image === null ? -1 : 1)
console.log(sorted)

Related

Change the property of all objects with javascript [duplicate]

This question already has answers here:
Modify object property in an array of objects
(12 answers)
Add property to all objects in array [duplicate]
(2 answers)
How to set specific property value of all objects in a javascript object array (lodash)
(3 answers)
javascript set all values in array of object
(4 answers)
Change properties of every item in an array?
(4 answers)
Closed 3 years ago.
I created an object with javascript and want to make the "completed" property of all objects true.
codes:
let todos = [
{
id: 0,
title: "Javascript",
completed: false
},
{
id: 1,
title: "php",
completed: false
},
]
I want to make the completed property of all objects true
function completeAll() {
//some codes
//I'm running with button
}
Use map.
let todos = [
{
id: 0,
title: "Javascript",
completed: false
},
{
id: 1,
title: "php",
completed: false
},
];
const output = todos.map(({completed, ...rest}) => ({...rest, completed: true}));
console.log(output);
Use . notation to access and change properties in the object
let todos = [
{
id: 0,
title: "Javascript",
completed: false
},
{
id: 1,
title: "php",
completed: false
},
]
todos.forEach(function(e){
e.completed=true;
})
console.log(todos)

Javascript return true if a value exists in a nested array [duplicate]

This question already has answers here:
Check if a value exists in an Array object in JavaScript or Angular
(4 answers)
Closed 3 years ago.
I want to return true if a nested array contains a particular value
In this example I'm trying to see if the users array has the current users id but I get the object instead of true
var currentUserId ="MBsCLlPbilRr26Jpz5oxhMULRvC2"
var users = [
{
id: "MBsCLlPbilRr26Jpz5oxhMULRvC2",
name: "Dennis",
url: undefined,
},
{
id: "CLlPbhMULRvC2jnjnDe",
name: "Dennis",
url: undefined,
},
]
console.log(users.find(user=>user.id === currentUserId))
The problem is you are using .find() instead of .some(). Try the following:
var currentUserId ="MBsCLlPbilRr26Jpz5oxhMULRvC2"
var users = [
{
id: "MBsCLlPbilRr26Jpz5oxhMULRvC2",
name: "Dennis",
url: undefined,
},
{
id: "CLlPbhMULRvC2jnjnDe",
name: "Dennis",
url: undefined,
},
]
console.log(users.some(user=>user.id === currentUserId))
The difference is in the output. .find() will return the value, .some() will return a boolean.

Converting an array of objects into an array [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.
I am trying to convert the "count" key from each object within the test array into a new array so I end up with something like
newCount =[0,0,0,0]
const test = [
{
id: 0,
count: 0,
image: "",
text: 'Some text about finn'
},
{
id: 1,
count: 0,
image: "",
text: 'Some text about daphne'
},
{
id: 2,
count: 0,
image: "",
text: 'Some text finn'
},
{
id: 3,
count: 0,
image: "",
text: 'Some text daphne'
}
]
You can use Array.prototype.map() on your test array to extract the count property value of each object:
const newCount = test.map(t => t.count);
Hopefully that helps!
You can use a map on the array:
const newarray = test.map(item => item.count);
Array map documentation

Find top level key in multidimensional object by value of second level key in javascript [duplicate]

This question already has answers here:
Find by key deep in a nested array
(21 answers)
Closed 5 years ago.
I have and multidimensional object that looks like this:
obj = {
'someString': {
name: 'John',
page: 'some url',
number: 4
},
'someString2': {
name: 'Bill',
page: 'some url',
number: 7
}
}
How do I find the first level key (in this case "someString2") where "number" is equal to 7?
The number is always unique and is the only thing I know beforehand.
Here you go. Using Array.find function to look for the appropriate key.
const numberToLookFor = 7;
const data = {
someString: {
name: 'John',
page: 'some url',
number: 4,
},
someString2: {
name: 'Bill',
page: 'some url',
number: 7,
},
};
const myKey = Object.keys(data).find(x => data[x].number === numberToLookFor);
console.log(myKey);

How to delete Object from deep javascript array? [duplicate]

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

Categories