Change the property of all objects with javascript [duplicate] - javascript

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)

Related

Insert object into dynamic key nested array [duplicate]

This question already has answers here:
Iterate through object properties
(31 answers)
Add property to an array of objects
(7 answers)
Closed 11 months ago.
Desired result: Insert an object into each dynamicItem and have the full obj object.
const obj = {
item1: {},
item2: {
dynamicItem1: [{ id: 'one' }, ], // add another prop here {type: 'added'}
dynamicItem2: [{ id: 'one' }, ] // add another prop here {type: 'added'}
},
}
My attempts: I managed to insert the object, but i can't think how to rebuild the full object and it also feels like i'm doing it wrong already.
const result = Object.keys(obj.item2).map(key => {
return obj.item2[key].map(item => ({ ...item, type: 'added' }))
})

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

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)

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

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