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

Categories

0 votes
2.1k 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 answer this question.

Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...