Javascript ES6/ES5 Znajdź w tablicy i zmień

Mam tablicę obiektów. Chcę znaleźć jakieś pole, a potem je zmienić:

var item = {...}
var items = [{id:2}, {id:2}, {id:2}];

var foundItem = items.find(x => x.id == item.id);
foundItem = item;

Chcę, aby zmienił oryginalny obiekt. Jak? (Nie obchodzi mnie czy też będzie w lodash)

Author: Mel, 2016-02-04

4 answers

Możesz użyć findIndex , aby znaleźć indeks w tablicy obiektu i zastąpić go zgodnie z wymaganiami:

var item = {...}
var items = [{id:2}, {id:2}, {id:2}];

var foundIndex = items.findIndex(x => x.id == item.id);
items[foundIndex] = item;

Zakłada to unikalne identyfikatory. Jeśli Twoje identyfikatory są zduplikowane( jak w twoim przykładzie), prawdopodobnie lepiej będzie, jeśli użyjesz forEach:

items.forEach((element, index) => {
    if(element.id === item.id) {
        items[index] = item;
    }
});
 83
Author: CodingIntrigue,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-02-04 16:20:59

Moje najlepsze podejście to:

var item = {...}
var items = [{id:2}, {id:2}, {id:2}];

items[items.findIndex(el => el.id === item.id)] = item;

Odniesienie do findIndex

I jeśli nie chcesz zastąpić nowym obiektem, ale zamiast tego skopiować pola item, możesz użyć Object.assign:

Object.assign(items[items.findIndex(el => el.id === item.id)], item)

Jako alternatywa z .map():

Object.assign(items, items.map(el=> el.id === item.id? item : el))

 19
Author: Soldeplata Saketos,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-04 11:08:23

Innym podejściem jest użycie splice .

W przypadku, gdy pracujesz z reaktywnymi frameworkami, zaktualizuje "widok", tablicę" wiedząc", że go zaktualizowałeś.

ODPOWIEDŹ:

var item = {...}
var items = [{id:2}, {id:2}, {id:2}];

let foundIndex = items.findIndex(element => element.id === item.id)
items.splice(foundIndex, 1, item)

I jeśli chcesz zmienić tylko wartość elementu, możesz użyć find function:

// Retrieve item and assign ref to updatedItem
let updatedItem = items.find((element) => { return element.id === item.id })

// Modify object property
updatedItem.aProp = ds.aProp
 6
Author: Toodoo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-07-29 20:05:30

Można zastosować filtr .

const list = [{id:0}, {id:1}, {id:2}];
let listCopy = [...list];
let filteredDataSource = listCopy.filter((item) => {
       if (item.id === 1) {
           item.id = 12345;
        }

        return item;
    });
console.log(filteredDataSource);

Array [Object { id: 0}, Object { id: 12345}, Object { id: 2}]

 0
Author: katwal-Dipak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-01 19:52:14