Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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


 

thumb_up_alt 0 like thumb_down_alt 0 dislike
2.1k views
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...