Iterate elements in multidimensional JSON array

Tell us what’s happening:

I m trying to iterate entire JSON array newData and want to update a field in it.

Your code so far

var newData=JSON.parse(success);
               
                console.log(newData)

               console.log(elementsName)
               
               const data1 = newData[0].results.recordset[0].ElementList;
              // console.log(data1.toArray());
              
               var array=JSON.parse(data1)
               array.forEach(function(element){
                if(element.ElementDescription==elementsName)
                {
                    element.ElementStatus="completed"
                }
                
               })
               
               console.log(array)

function filteredArray(arr, elem) {
  let newArr = [];
  // change code below this line

  // change code above this line
  return newArr;
}

// change code here to test different cases:
console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/iterate-through-all-an-arrays-items-using-for-loops/

Why update a field? A challenge is asking you to remove sub arrays which contain certain elements. For example, it says:

filteredArray([ ["trumpets", 2], ["flutes", 4], ["saxophones", 2] ], 2) should return [ ["flutes", 4] ]

^ 2 at the end is a condition. Therefore, it removed the first array and the last array which contained 2.

The challenge already created a new variable newArr to store updated arrays in. What you should do is

  1. Iterate through an array with a for loop https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration#for_statement
  2. As a condition, use indexOf applying to an element. If the index = -1, it means that there is no such element. Therefore, we can execute a program further.
  3. To store filtered arrays in a new variable, use .push https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.

markdown_Forums

So your question is unrelated to the challenge? Seems like an odd way to make a thread.

Without knowing the data structure and seeing all of the code it’s kind of hard to help.
But you seem to be parsing an array here and not a JSON string.

var array = JSON.parse(data1)