javascript: loop through object array and pushing elements into one array

Dave

I am trying to loop through an object array (data.list) and pull out an element (required_fields.display_value) and push them into another array and concatenate.

I've written the following:

c.data.required_fields = [];
            for(var i=0; i<c.data.list.length; i++) {
                c.data.required_fields.push(
                    c.data.list[i].required_fields.display_value.split(',')             
                );
            }

which returns this:
enter image description here

What do I have to add to my code above so that required_fields is a single array? Thanks!

Devin Fields
c.data.required_fields = [];
for(var i=0; i<c.data.list.length; i++) {
    c.data.required_fields = c.data.required_fields.concat(
        c.data.list[i].required_fields.display_value.split(',')             
    );
}

This should do the trick. Since every call to .split will return an array, you need to concat the contents of that array into the required_fields array. Concat returns a branch new array, however, hence the "c.data.required_fields = c.data.required_fields.concat..." assignment.

**This is a very simple fix. You could of course do something more readable with reduce, but I believe another answer has that covered.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Create a javascript object and then fill an array through a for loop

分類Dev

Pushing an Object to the array

分類Dev

Pushing element into array inside for loop

分類Dev

Code to loop through an array in one line in Ruby

分類Dev

Pushing arrays of one variable in one array

分類Dev

Putting elements in an array from for loop in Javascript

分類Dev

How can I iterate through an object array and add matching values while pushing new values

分類Dev

Can I loop through an array to change and elements attributes on hover

分類Dev

combine multiple array elements into single object in javascript

分類Dev

How to count certain elements in an object array in JavaScript?

分類Dev

Infinite loop when pushing into array using UseState()?

分類Dev

loop through implicit array

分類Dev

Pushing items into an array if dates matches - JavaScript/TypeScript

分類Dev

Pushing to a Javascript array that has a __proto__

分類Dev

Array of Array into Array of object in javascript

分類Dev

How to loop through object - JavaScript

分類Dev

React: Loop through array of strings

分類Dev

Struggling to loop through an array of objects

分類Dev

Array validation through a while loop

分類Dev

Perl: pushing elements into array replaces existing value with new variable value

分類Dev

javascript change elements in array

分類Dev

Sort array elements on JavaScript

分類Dev

how to use snap.svg to loop through svg elements and create array of each ones path attribute

分類Dev

Why does Ember complain when pushing an object onto an Ember array?

分類Dev

Pushing accelerometer data into an array

分類Dev

ng-repeat angular adding two elements to a div as opposed to one when iterating through array

分類Dev

Access JSON Array through Javascript

分類Dev

Loop through array: CCAction for every element, but only one callback after actions are finished

分類Dev

Object in array is not updating in JavaScript

Related 関連記事

  1. 1

    Create a javascript object and then fill an array through a for loop

  2. 2

    Pushing an Object to the array

  3. 3

    Pushing element into array inside for loop

  4. 4

    Code to loop through an array in one line in Ruby

  5. 5

    Pushing arrays of one variable in one array

  6. 6

    Putting elements in an array from for loop in Javascript

  7. 7

    How can I iterate through an object array and add matching values while pushing new values

  8. 8

    Can I loop through an array to change and elements attributes on hover

  9. 9

    combine multiple array elements into single object in javascript

  10. 10

    How to count certain elements in an object array in JavaScript?

  11. 11

    Infinite loop when pushing into array using UseState()?

  12. 12

    loop through implicit array

  13. 13

    Pushing items into an array if dates matches - JavaScript/TypeScript

  14. 14

    Pushing to a Javascript array that has a __proto__

  15. 15

    Array of Array into Array of object in javascript

  16. 16

    How to loop through object - JavaScript

  17. 17

    React: Loop through array of strings

  18. 18

    Struggling to loop through an array of objects

  19. 19

    Array validation through a while loop

  20. 20

    Perl: pushing elements into array replaces existing value with new variable value

  21. 21

    javascript change elements in array

  22. 22

    Sort array elements on JavaScript

  23. 23

    how to use snap.svg to loop through svg elements and create array of each ones path attribute

  24. 24

    Why does Ember complain when pushing an object onto an Ember array?

  25. 25

    Pushing accelerometer data into an array

  26. 26

    ng-repeat angular adding two elements to a div as opposed to one when iterating through array

  27. 27

    Access JSON Array through Javascript

  28. 28

    Loop through array: CCAction for every element, but only one callback after actions are finished

  29. 29

    Object in array is not updating in JavaScript

ホットタグ

アーカイブ