Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (100 points)

Update Object values Javascipt / typescript

Hello, how can I updated nested child value in Object -> example Variable is 2 & Item VariableGroup is 2 so new value will be 999 instead of 160.
It should also work for changing others in case helperFunction is called example (1,1,55).
var data = [
  {
    "Variable": 1,
    "Item": [
      {
        "VariableGroup": 1,
        "VariableValue": 190
      },
      {
        "VariableGroup": 2,
        "VariableValue": 38
      }
    ]
  },
  {
    "Variable": 2,
    "Item": [
      {
        "VariableGroup": 1,
        "VariableValue": 80
      },
      {
        "VariableGroup": 2,
        "VariableValue": 160
      }
    ]
  },
  {
    "Variable": 3,
    "Item": [
      {
        "VariableGroup": 1,
        "VariableValue": 900
      },
      {
        "VariableGroup": 2,
        "VariableValue": 1200
      }
    ]
  },  
]
 
function helperFx (variable: number, variablegroup: number, variablevalue: number)
{
  let newdata = [...data];
 
  for(let item of newdata){
    if (item.Variable === variable)
    {
      for(let v of item.Item )
      {
        if(v.VariableGroup === variablegroup)
        {
          console.log(v);
          break;
        }
      }
    }
  }
 
 
};
 
helperFx(2,2,999);


 

Welcome To Ask or Share your Answers For Others

Please log in or register to reply this article.

OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...