How to add values ​​to an array using for?

  • 0
    Here I want to add an image array to the data that comes from ajax for further processing. I tried different options, but for some reason it only writes the last value from the people array.
    Tried these ways
    data['image'] = [people[i] ];
    data['image'] = {i: people[i]};

    Adds only the last value of Sam, tell me how to properly add values ​​from people to the data ['image'] array
    let data = ['Zna4', 'Zna4', 'Zna4'];
    
    let people = ["Tom", "Alice", "Sam"];
    
    
    for (let i=0; i < 3; i++) { 
      data['image'] = {i: people[i]};
    }
    console.log(data);
    JavaScript Anonymous, Aug 26, 2019

  • 1 Answers
  • 0
    Everything is much simpler.



    let data = ['Zna4', 'Zna4', 'Zna4'];
    let people = ["Tom", "Alice", "Sam"];

    data['image'] = people




    With the amendment that data ceases to be an iterable numeric array and becomes an object with string keys.
    Lilah Crane

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!