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(',')
);
}
What do I have to add to my code above so that required_fields is a single array? Thanks!
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]
コメントを追加