如何在数组中更新json对象?

时间:2021-07-05 07:22:09

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. My console.log reads "Found undefined".

然而,这并不奏效。我找到了对象,但是obj。任何事情都是未定义的。我的控制台。日志读取“发现未定义”。

5 个解决方案

#1


1  

EVEN SIMPLER

更简单的

You could use the some operator. (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
   }
});

#2


2  

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数组,第二个映射将循环遍历这些值和控制台。日志(“发现”+ object .title)将打印“发现+未定义”。

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;
 });

#3


1  

Change it to some other variable instead of obj since it refers to parent obj which is an array. Also use array.filter in the first function that will return an array,

将它改为其他变量,而不是obj,因为它引用的是父obj,它是一个数组。也使用数组。第一个函数的过滤器将返回一个数组,

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;
 });

#4


1  

Map is used to return a mutated object. When using map you need to return something back, in your case modified object.

映射用于返回一个有突变的对象。在使用映射时,您需要返回一些东西,在您的案例中是修改后的对象。

However There are things you're doing wrong. 1) you're using .map to search something (thats not how you use map); try using .filter or .find 2) after updating your object using map (in 2nd function), you need to return it back. case 1:

然而有些事情你做错了。你正在使用.map搜索某些东西(这不是你使用地图的方式);尝试使用.filter或.find 2)在使用map(在第二个函数中)更新对象之后,您需要返回它。案例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;

#5


1  

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。标题真的,那么数组。过滤是你需要的

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

#1


1  

EVEN SIMPLER

更简单的

You could use the some operator. (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
   }
});

#2


2  

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数组,第二个映射将循环遍历这些值和控制台。日志(“发现”+ object .title)将打印“发现+未定义”。

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;
 });

#3


1  

Change it to some other variable instead of obj since it refers to parent obj which is an array. Also use array.filter in the first function that will return an array,

将它改为其他变量,而不是obj,因为它引用的是父obj,它是一个数组。也使用数组。第一个函数的过滤器将返回一个数组,

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;
 });

#4


1  

Map is used to return a mutated object. When using map you need to return something back, in your case modified object.

映射用于返回一个有突变的对象。在使用映射时,您需要返回一些东西,在您的案例中是修改后的对象。

However There are things you're doing wrong. 1) you're using .map to search something (thats not how you use map); try using .filter or .find 2) after updating your object using map (in 2nd function), you need to return it back. case 1:

然而有些事情你做错了。你正在使用.map搜索某些东西(这不是你使用地图的方式);尝试使用.filter或.find 2)在使用map(在第二个函数中)更新对象之后,您需要返回它。案例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;

#5


1  

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。标题真的,那么数组。过滤是你需要的

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