1

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
} 
Atakan
  • 91
  • 1
  • 8

2 Answers2

3

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);
random
  • 7,756
  • 3
  • 19
  • 25
1

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)
ellipsis
  • 12,049
  • 2
  • 17
  • 33