简体   繁体   English

如何更新数组内的json对象?

[英]How can I update a json object inside of an array?

I have an array of objects and I want to update some of the content. 我有一个对象数组,我想更新一些内容。 I figured I could just map through the objects, find the match I was looking for and than update it. 我想我可以映射对象,找到我要寻找的匹配项,然后更新它。

data = data.map(obj => {
   return this.state.objToFind === obj.title;   
   }).map(obj, idx) => {
      console.log("found " + obj.title);     // reads found + undefined?
      obj.menu = this.state.menu;
      obj.title = this.state.title;
      obj.content = this.state.content;
 });

However, this is not working. 但是,这不起作用。 I find the object but obj.anything is undefined. 我找到了对象,但obj.anything是未定义的。 My console.log reads "Found undefined". 我的console.log读取“发现未定义”。

data.map(obj => {
  return this.state.objToFind === obj.title;   
})

the first map will return an array of true and false, the second map will loop through these values and the console.log("found " + obj.title) will prints "found + undefined" consequently. 第一个映射将返回true和false的数组,第二个映射将循环遍历这些值, console.log("found " + obj.title)将因此显示“ found + undefined”。

maybe you wanted something like this. 也许您想要这样的东西。

data = data.filter(obj => {
   return this.state.objToFind === obj.title;   
   }).map(obj, idx) => {
      console.log("found " + obj.title);
      obj.menu = this.state.menu;
      obj.title = this.state.title;
      obj.content = this.state.content;
      return obj;
 });

Change it to some other variable instead of obj since it refers to parent obj which is an array . 将其更改为其他变量而不是obj,因为它引用的是an arrayobj Also use array.filter in the first function that will return an array, 还要在第一个函数中使用array.filter,它将返回一个数组,

data = data.filter(obj => {
   return this.state.objToFind === obj.title;   
   }).map(newval, idx) => {
      console.log("found " + newval.title);     
      newval.menu = this.state.menu;
      newval.title = this.state.title;
      newval.content = this.state.content;
 });

Map is used to return a mutated object. Map用于返回变异的对象。 When using map you need to return something back, in your case modified object. 使用map时,您需要返回一些东西,以您的情况为修改对象。

However There are things you're doing wrong. 但是,有些事情您做错了。 1) you're using .map to search something (thats not how you use map); 1)您正在使用.map搜索内容(这不是您使用map的方式); try using .filter or .find 2) after updating your object using map (in 2nd function), you need to return it back. 尝试使用.filter或.find 2)使用map更新对象(在第二个函数中)后,您需要将其返回。 case 1: 情况1:

data = data.filter(obj => {
       return this.state.objToFind === obj.title;   
   }).map(foundObj, idx) => {
          console.log("found " + foundObj.title);
          foundObj.menu = this.state.menu;
          foundObj.title = this.state.title;
          foundObj.content = this.state.content;
      return foundObj; //updated obj
 });

case 2: 情况2:

    var foundObj = data.find(obj => {
     return this.state.objToFind === obj.title;   
   });
    console.log("found " + foundObjs.title);
    foundObjs.menu = this.state.menu;
    foundObjs.title = this.state.title;
    foundObjs.content = this.state.content;

If you want to process only those elements which makes this.state.objToFind === obj.title true then Array.filter is what you need like 如果您只想处理使this.state.objToFind === obj.title true的那些元素,那么Array.filter是您所需要的

data = data.filter(obj => {
   return this.state.objToFind === obj.title;   
   }).map(obj, idx) => {
      console.log("found " + obj.title);     // reads found + undefined?
     ...
 });

EVEN SIMPLER 甚至简单

You could use the some operator. 您可以使用some运算符。 (It works by iterating over the array, when you return true it breaks out of the loop) (它通过遍历数组来工作,当您返回true时,它会跳出循环)

data.some(function(obj){
   if (obj.title ==== 'some value'){
        //change the value here
        obj.menu = 'new menu';
        obj.title = 'new title';
        return true;    //breaks out of he loop
   }
});

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

相关问题 如果我在对象中有一个数组并且该对象在一个大数组中,我该如何更新 React Native 状态? - How can I update the React Native state if I have an array inside an object and the object is inside in a big array? 如何使用 Redux 更新对象数组中的状态? - How can I update a state inside an array of object with Redux? 对象内部的JSON更新数组 - JSON update array inside of object 我想 JSON 解析对象内的对象数组 - 正文中的文章,我该怎么做? - I want to JSON Parse an array of objects inside an object - articles from the body, how can I do that? 如何在 Nodejs 的数组中更新 object? - How can I update object in an array in Nodejs? 我如何将 .map() 和 Array 嵌套在一个对象内,在一个数组内? - How Can I .map() and Array that is nested inside an object, inside an array? 如何使用NodeJs Express在车把的JSON中显示数组中的对象 - How can I show object inside array in JSON in handlebars with NodeJs Express 我如何使用 setState 更新数组内的对象 - How do i use setState to update an object inside an array 我如何在 React 的数组中更新 object 中的键值? - How do i update value of a key in an object inside an Array in React? 如何在 redux 中更新 object 内的数组 - How do I update array inside object in redux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM