简体   繁体   中英

Javascript loop an array object and import into another file

First thing first, I don't know exactly how to describe this situation. But, I want to define an object to minimize the code. Here's the example:

let addtionalStatus = {
    ports: [
        1: {
            someKeys: "",
            someOtherKeys: ""
        },
        2: {
            someKeys: "",
            someOtherKeys: ""
        },
        3: {
            someKeys: "",
            someOtherKeys: ""
        }
        n: {
            someKeys: "",
            someOtherKeys: ""
        }
    ]
}

So, this is my code:

let additionalStatus = {
    ports: []
};

for(let i; i <= n; i++){
    additionalStatus.ports[i].someKeys = "";
    additionalStatus.ports[i].someOtherKeys = "";
}

export {additionalStatus};

the code above showing an error:

TypeError: additionalStatus.ports[i] is undefined

You should be doing this instead.

let additionalStatus = {ports: []}
for (let i = 0; i < 10; i++) {
  additionalStatus.ports.push({
    someKeys: "",
    someOtherKeys: ""
  });  
}
console.log(additionalStatus);    

// OUTPUT
{                                                
  ports: [                                       
    { someKeys: '', someOtherKeys: '' },         
    { someKeys: '', someOtherKeys: '' },         
    { someKeys: '', someOtherKeys: '' },         
    { someKeys: '', someOtherKeys: '' },         
    { someKeys: '', someOtherKeys: '' },         
    { someKeys: '', someOtherKeys: '' },         
    { someKeys: '', someOtherKeys: '' },         
    { someKeys: '', someOtherKeys: '' },         
    { someKeys: '', someOtherKeys: '' },         
    { someKeys: '', someOtherKeys: '' }          
  ]                                              
}                                                

I Will recommend you to define empty entries instead. Row data is easier to read at any time of the day.

let additionalStatus =  {                                                
      ports: [                                       
        { someKeys: '', someOtherKeys: '' },         
        { someKeys: '', someOtherKeys: '' },         
        { someKeys: '', someOtherKeys: '' },         
        { someKeys: '', someOtherKeys: '' },         
        { someKeys: '', someOtherKeys: '' },         
        { someKeys: '', someOtherKeys: '' },         
        { someKeys: '', someOtherKeys: '' },         
        { someKeys: '', someOtherKeys: '' },         
        { someKeys: '', someOtherKeys: '' },         
        { someKeys: '', someOtherKeys: '' }          
      ]                                              
    } 

There are some errors in your code.

  1. additionalStatus.ports[i] is undefined because you just define i in the loop, but don't assign value to it. Thats why i is undefined .
  2. How you structure your data isn't correct. You can't define object with key:value pair directly into an array . Ex-
 // ITS NOT CORRECT const arr = [ 1: { name: 'something' }, 2: { name: 'anything' } ];

Now the question is how you should structure your data and loop through it ? You can follow as i mentioned bellow like this -

const additionalStatus = {
    ports: [
      { someKeys: '', someOtherKeys: '' },
      { someKeys: '', someOtherKeys: '' }
    ]
}

for(let itemInArray of additionalStatus.ports) {
  console.log(itemInArray.someKeys);      // value of `someKeys`
  console.log(itemInArray.someOtherKeys); // value of `someOtherKeys`
}

NOTE:

To loop through an array: use for-of

To loop through an object: use for-in

the error is because you haven't assign any value to array [i], it should assign 0 and n shouls be

additionalStatus.ports.length - 1

other wise it will give you error TypeError: additionalStatus.ports[i] is undefined *

check this

for(let i = 0; i < additionalStatus.ports.length - 1, i++ ) {

additionalStatus.ports[i].someKeys = "";
    additionalStatus.ports[i].someOtherKeys = "";
}

hope it will solve your issue

Try this one:

let additionalStatus = {
    ports: []
};

let ports = [];

for (let i = 1; i <= n; i++) {
    let obj = {
        someKeys: false,
        someOtherKeys: ''
    };
    ports.push(i);
    ports[i] = obj;
}

additionalStatus.ports = ports;

export { additionalStatus };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM