简体   繁体   English

JAVASCRIPT:更改对象 ID 匹配的对象列表中的密钥对

[英]JAVASCRIPT: Change the key pair in a list of objects where the object ID matches

I have an object as follows我有一个对象如下

{
  "id":34567,
  "description":"The description goes here",
  "favorite":false,
},
{
  "id":34569,
  "description":"The description goes here",
  "favorite":false,
}

My question is, what JavaScript is required to change the 'favorite' value ONLY where the id = 34567我的问题是,只有在 id = 34567 的情况下才需要什么 JavaScript 才能更改“最喜欢的”值

So the result will become ;所以结果会变成 ;

{
  "id":34567,
  "description":"The description goes here",
  "favorite":false,
},
{
  "id":34569,
  "description":"The description goes here",
  "favorite":true,
}

You can filter it out from the array, and change the keys value of the first elemnt of the array of filtered elements.您可以将其从数组中过滤掉,并更改过滤元素数组的第一个元素的键值。 Like here:像这儿:

 const data = [{ "id":34567, "description":"The description goes here", "favorite":false, }, { "id":34569, "description":"The description goes here", "favorite":false, }]; console.log(data); const addFavorite = (id) => { data.filter((d) => d.id==id)[0].favorite = true; } addFavorite(34569); console.log(data);

Or you can use the find method if tha ID's are unique, as或者,如果 ID 是唯一的,您可以使用 find 方法,如
@cmgchess mentioned below in the comments section. @cmgchess 在下面的评论部分中提到。

 const data = [{ "id":34567, "description":"The description goes here", "favorite":false, }, { "id":34569, "description":"The description goes here", "favorite":false, }]; console.log(data); const addFavorite = (id) => { data.find((d) => d.id==id).favorite = true; } addFavorite(34569); console.log(data);

And you can map through all the filtered elements if the ID isn't unique.如果 ID 不是唯一的,您可以映射所有过滤的元素。

 const data = [{ "id":34567, "description":"The description goes here", "favorite":false, }, { "id":34569, "description":"The description goes here", "favorite":false, }, { "id":34569, "description":"It is a copy", "favorite":false, } ]; console.log(data); const toggleFavorite = (id) => { data.filter((d) => d.id==id).map((d)=>d.favorite = !d.favorite); } toggleFavorite(34569); console.log(data);

you can use map function for this.您可以为此使用地图功能。

data = data.map(d => {
    if(d.id === 34569) {
        d.favorite = true;
    }
    return d;
});

this will also work if there are multiple objects in array which satisfy the condition.如果数组中有多个满足条件的对象,这也将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将键值对添加到key为对象的javascript对象 - Adding key-value pair to javascript object where key is an object 将嵌套键值对添加到 Javascript object 中,其中键是动态的 - add a nested key value pair to a Javascript object where key is dynamic Javascript-具有键值对的对象,其中value是一个数组 - Javascript - object with key value pair, where value is an array 从密钥与javascript和下划线匹配特定条件的对象返回键值对 - return key value pair from object whose key matches certain condition with javascript and underscore 如何在 JavaScript 中更改对象键值对的位置 - How to change the location of an object key value pair in JavaScript 如何在Javascript中将多个项目列表减少为键值对象? - How to reduce a list of multiple items to a key value pair object in Javascript? Javascript,按ID在对象列表中查找对象 - Javascript, find object in list of objects by id Javascript 将对象列表转换为具有键值的 object - Javascript transform list of objects into object with key value 使用jQuery迭代javascript中的关联数组(键/值对),其中值是jQuery对象 - Iterating an associative array (key/value pair) in javascript with jQuery, where the values are jQuery objects 如何根据每个对象id为数组中的每个对象添加一个密钥对值? - How to add to each object in an array a key-pair value based on each objects id?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM