0

I have a array of objects. I want to update an object using id.

I am able to do using the map function. Is there an alternative way or more efficient way to update the array?

Here is my code: https://stackblitz.com/edit/js-xgfwdw?file=index.js

var id = 3
var obj = {
  name: "test"
}
let arr = [{
  name: "dd",
  id: 1
}, {
  name: "dzxcd",
  id: 3
}, {
  name: "nav",
  id: 5
}, {
  name: "hhh",
  id: 4
}]

function getUpdated(obj, id) {
  var item = [...arr];
  const t = item.map((i) => {
    if(i.id==id){
      return {
        ...obj,
        id
      }
    }else {
      return i;
    }
  })
  return t
}

console.log(getUpdated(obj,id))

The expected output is correct but I want to achieve the same functionality using an alternative way.

[{
      name: "dd",
      id: 1
    }, {
      name: "test",
      id: 3
    }, {
      name: "nav",
      id: 5
    }, {
      name: "hhh",
      id: 4
    }]
1
  • I don't see anything wrong with this. Maybe use a ternary instead of if/else? But that would be a matter of syntax preference. Commented Jan 18, 2019 at 16:21

4 Answers 4

4

you are in the correct way, basically the bad thing that you are doing is creating new arrays [...arr], when map already gives you a new array.

other things to use, may be the ternary operator and return directly the result of the map function

check here the improvedGetUpdate:

var id = 3;

var obj = {
  name: "test"
};

let arr = [{
  name: "dd",
  id: 1
}, {
  name: "dzxcd",
  id: 3
}, {
  name: "nav",
  id: 5
}, {
  name: "hhh",
  id: 4
}]

function getUpdated(obj, id) {
  var item = [...arr];
  const t = item.map((i) => {
    if (i.id == id) {
      return {
        ...obj,
        id
      }
    } else {
      return i;
    }
  })
  return t
}

improvedGetUpdate = (obj, id) => arr.map(i => {
  return i.id !== id ? i : {
    ...obj,
    id
  }
})

console.log(getUpdated(obj, id))
console.log(improvedGetUpdate(obj, id))

Sign up to request clarification or add additional context in comments.

Comments

2

var id = 3
var obj = {
  name: "test"
}
let arr = [{
  name: "dd",
  id: 1
}, {
  name: "dzxcd",
  id: 3
}, {
  name: "nav",
  id: 5
}, {
  name: "hhh",
  id: 4
}]

const result = arr.map((el) => el.id === id ? {...obj, id} : el)

console.log(result);

Comments

1

Use splice method which can be used to update the array too:

var obj = {
  id: 3,
  name: "test"
}
let arr = [{
  name: "dd",
  id: 1
}, {
  name: "dzxcd",
  id: 3
}, {
  name: "nav",
  id: 5
}, {
  name: "hhh",
  id: 4
}]

arr.splice(arr.findIndex(({id}) => id === obj.id), 0, obj);
console.log(arr);

Comments

0

@quirimmo suggested short code. I suggest fast code.

var id = 3;
var obj = {
  id:   3,
  name: "test"
}
let arr = [{
  name: "dd",
  id: 1
}, {
  name: "dzxcd",
  id: 3
}, {
  name: "nav",
  id: 5
}, {
  name: "hhh",
  id: 4
}]
var arr2 = [...arr];
console.time('⏱');
arr.splice(arr.findIndex(({id}) => id === obj.id), 0, obj);
console.timeEnd('⏱');
console.time('⏱');
for (let item of arr2) {
  if (item.id === id) {
    item.name = obj.name;
    break;
  }
}
console.timeEnd('⏱');
console.log(arr2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.