0

I am getting a little confused. I have the following array of objects:

dataTest.rows = [{
        "rowHeader": "",
        "rowDesc": ["INFO1"],
        "rowVal": [
            ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
        ]
    }, {
        "rowHeader": "",
        "rowDesc": ["INFO2"],
        "rowVal": [
            ["1", "2", "3", "4", "5", "6", "7", "8", "9"]

        ]
    }];

How would I fill up the array within the array rowval with a loop if rowVal, say in object1, was empty?

I tried

for (i=0; i<9; i++){
  (dataTest.rows[0]).rowVal[0][i]=i;
}

Should I completely remove the label 'rowVal', and then add it to the object to rebuild it?

7
  • wasn't quite get the question here, all you need is replace all the item in rowVal with same value of index? Commented Apr 20, 2018 at 1:31
  • And whats the result of your loop? Commented Apr 20, 2018 at 1:32
  • You need to convert the number to a string in your loop - the example you gave contains strings and not numbers. Also "if rowVal, say in object1, was empty", you'd need to create an array first to modify. Commented Apr 20, 2018 at 1:32
  • Your question just doesn't make sense. What do you mean by 'was empty?' at the end? Do you mean if rowVal had no arrays? Commented Apr 20, 2018 at 1:39
  • Please edit your question and show us what you want the data to look like after the loop Commented Apr 20, 2018 at 1:40

2 Answers 2

1

var dataTest={};
dataTest.rows = [{
        "rowHeader": "",
        "rowDesc": ["INFO1"],
        "rowVal": [[]]
    }, {
        "rowHeader": "",
        "rowDesc": ["INFO2"],
        "rowVal": [[]]
    }];
for (var i=0; i<dataTest.rows.length; i++){
    // iteriates through the dataTest.rows array of objects
    console.log("Entered row %i",i);
    for (var j=1; j<=9; j++){
        // converts number to string
        dataTest.rows[i].rowVal[0][j]=j.toString();
    	  console.log(dataTest.rows[i].rowVal[0][j]);
    }
}

UPDATE: Fixed "dataTest.rows[i].rowVal[0] is undefined."

Sign up to request clarification or add additional context in comments.

3 Comments

dataTest.row[i].rowVal[0] is undefined?
@ApisMel I fixed that. It needs to have the sub-array included.
cool! thanks for helping.. this is working great.. now i can go to the next step.. get the data from a json file
0

You can use the .forEach() Array method to loop through every item in array X and push it into array Y.

let dataTest= [{
    "rowHeader": "",
    "rowDesc": ["INFO1"],
    "rowVal": []
}, 

{
    "rowHeader": "",
    "rowDesc": ["INFO2"],
    "rowVal": [
        ["1", "2", "3", "4", "5", "6", "7", "8", "9"]

    ]
}];

dataTest[1].rowVal.forEach(number => {
                dataTest[0].rowVal.push(number)
                })

In this example, the forEach function runs through every item in the dataTest[1].rowVal and pushes everything inside into the array in dataTest[0].rowVal, which should achieve what you're asking.

2 Comments

what if both array are empty.
You can run a for loop before the forEach function, where i = 1 and the second argument is i < X, where X is the top end of the range.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.