admin管理员组文章数量:1415491
The async/await function search
returns an object similar to:
[
{
"title":"page title",
"url":"/page/url/index.html",
"content":"blah blah blah"
},
...
]
I need to push each object from val
into the array, but when I push using:
results = [];
search(input).then(val=> results.push(val));
I get a nested array similar to (I want to the object entries to be top level in the array):
[
[
{title:"...", url:"...", content:"..."},
{title:"...", url:"...", content:"..."},
]
]
So I tried a for
in the arrow to push each entry in the object into the array, but that didn't seem to work.
results = [];
search(input).then(val=> for(i in val) {results.push(val[i])});
The async/await function search
returns an object similar to:
[
{
"title":"page title",
"url":"/page/url/index.html",
"content":"blah blah blah"
},
...
]
I need to push each object from val
into the array, but when I push using:
results = [];
search(input).then(val=> results.push(val));
I get a nested array similar to (I want to the object entries to be top level in the array):
[
[
{title:"...", url:"...", content:"..."},
{title:"...", url:"...", content:"..."},
]
]
So I tried a for
in the arrow to push each entry in the object into the array, but that didn't seem to work.
results = [];
search(input).then(val=> for(i in val) {results.push(val[i])});
Share
Improve this question
asked Aug 19, 2020 at 14:23
JasonJason
7,69015 gold badges79 silver badges129 bronze badges
3
- 4 Try this: search(input).then(val=> results.push(...val)); – Patrick Hund Commented Aug 19, 2020 at 14:26
-
1
Can't you just use
results = val
? although I don't really see a purpose of doing this as you'll only be able to access the populated version ofresults
once your promise has resolved, such as in the.then()
callback, so you might as well just useval
– Nick Parsons Commented Aug 19, 2020 at 14:27 -
Try
search(input).then(val=> [].concat(results, val));
– Kunal Mukherjee Commented Aug 19, 2020 at 14:33
4 Answers
Reset to default 3Instead of .push()
, use .concat()
results = [];
search(input).then(val=> { results = results.concat(val) });
Use spread syntax ...
No For loop
needed either.
search(input).then(val=> results = [...results,...val]);
or as @Patrick_Hund suggested
search(input).then(val=> results.push(...val));
here problem is that you are pushing array in to an array.
You need to push the elements of array.
Spreading syntax should fix the issue.
Hope you have clear understanding now.
If you have an array and call a function that will return Promise<array>
you don't need to perform any iteration on the result. Concatenate the arrays:
async function getNewData() {
return Promise.resolve([{
d: 1
}, {
e: 3
}]);
}
let results = [{
a: 3
}, {
b: 4
}];
getNewData().then(newdata => {
results = results.concat(newdata);
// same as results = [...results, ...newdata];
console.log(results);
});
On the other hand, you could also push using the spread operator
async function getNewData() {
return Promise.resolve([{
d: 1
}, {
e: 3
}]);
}
const results = [{
a: 3
}, {
b: 4
}];
getNewData().then(newdata => {
results.push(...newdata);
// same as results.push(newdata[0], newdata[1], etc)
console.log(results);
});
This approach might be confusing as people tend to ignore the variadic nature of Array.prototype.push, but it avoids redeclaring results
, so you may declare the target array as const.
If you want to use a for loop, you'll have to do
for(var i=0; i < val.length; i++) {
results.push(val[i])
}
There are more succinct ways to do this (see Patrick Hund's ment), but that's how you do it with a for loop.
本文标签: javascriptFrom arrow functionpush object to arrayStack Overflow
版权声明:本文标题:javascript - From arrow function, push object to array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745178900a2646372.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论