英文:
Map not returning array as expected from axios
问题
const func = async (vars, update_vars) => {
await axiosFunction({ query: create_fields, variables: vars }).then(
(response) => {
if (update_vars != undefined) {
const yo = update_vars.map((att) => {
if (att.label === response.data.label)
return response.data.id;
});
console.log(yo);
}
}
);
};
update_vars
格式:
[
{ label: 'val 1' },
{ label: 'val 2' },
{ label: 'val 3' },
{ label: 'val 4' }
]
response.data
格式
{
id: '112',
other_id: '352009093',
label: 'val 1'
}
{
id: '113',
other_id: '3520012',
label: 'val 2'
}
{
id: '152',
other_id: '234578',
label: 'val 13'
}
我需要 yo
返回 [ '112', '113' ]
<details>
<summary>英文:</summary>
I have an axios post call to a graphQL mutation. In response, I need to create and array but I am getting so many `undefined` values.
My code:
const func = async (vars, update_vars) => {
await axiosFunction({ query: create_fields, variables: vars }).then(
(response) => {
if (update_vars != undefined) {
const yo = update_vars.att.map((att) => {
if (
att.label === response.data.label
)
return response.data.id;
});
console.log(yo);
}
}
);
};
`update_vars` format:
[
{ label: 'val 1' },
{ label: 'val 2' },
{ label: 'val 3' },
{ label: 'val 4' }
]
`response.data` format
{
id: '112',
other_id: '352009093',
label: 'val 1'
}
{
id: '113',
other_id: '3520012',
label: 'val 2'
}
{
id: '152',
other_id: '234578',
label: 'val 13'
}
I need `yo` to return `['112', '113']`
</details>
# 答案1
**得分**: 2
你可以创建一个`Set`来存储`update_vars`中对象的所有标签属性,然后基于这些属性来进行响应的`filter`操作。最后,可以使用`Array#map`来提取匹配元素的id。
```javascript
let update_vars = [{label: "val 1"}, {label: "val 2"}, {label: "val 3"}, {label: "val 4"}];
let response = {
data: [
{id: "112", other_id: "352009093", label: "val 1"},
{id: "113", other_id: "3520012", label: "val 2"},
{id: "152", other_id: "234578", label: "val 13"}
]
};
const labels = new Set(update_vars.map(o => o.label));
const res = response.data.filter(({label}) => labels.has(label)).map(o => o.id);
console.log(res);
英文:
You can create a Set
of all the label properties in the objects in update_vars
, then filter
the response based on them. Finally, Array#map
can be used to extract the ids of the matching elements.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
let update_vars=[{label:"val 1"},{label:"val 2"},{label:"val 3"},{label:"val 4"}],response={data:[{id:"112",other_id:"352009093",label:"val 1"},{id:"113",other_id:"3520012",label:"val 2"},{id:"152",other_id:"234578",label:"val 13"}]};
const labels = new Set(update_vars.map(o => o.label));
const res = response.data.filter(({label}) => labels.has(label)).map(o => o.id);
console.log(res);
<!-- end snippet -->
答案2
得分: 1
The code defines an array of arrays called update_var
which contains two rows of objects, each with four label keys and their corresponding string values.
Then a data
array is defined, which contains objects with an id
, other_id
, and label
keys with their corresponding string values.
A new array is created by using the flat()
method on the update_var
array, which flattens the nested arrays into a single array.
An empty array called yo
is declared.
The code then iterates through each element of the flattened arrayFlat
using a forEach()
loop and inside the loop it filters the data
array by matching the label
key of each object with the label
key of the current element in arrayFlat
.
If there is a match, it pushes the value of the id
key of the first element of the filtered array to the yo
array.
Finally, the yo
array is logged to the console.
This code essentially filters an array of objects (data
) based on a list of labels from a nested array (update_var
). Then it outputs the corresponding id
values.
英文:
If I understood your problem correctly, the solution should be something like this, obviously it can be improved.
const update_var = [[
{ label: 'val 1' },
{ label: 'val 2' },
{ label: 'val 3' },
{ label: 'val 4' }
],
[
{ label: 'val 5' },
{ label: 'val 6' },
{ label: 'val 7' }
]];
var data = [{
id: '112',
other_id: '352009093',
label: 'val 1'
},
{
id: '113',
other_id: '3520012',
label: 'val 2'
},
{
id: '152',
other_id: '234578',
label: 'val 13'
}]
const arrayFlat = update_var.flat()
const yo = []
arrayFlat.forEach(el => {
const tmp = data.filter(e => el.label === e.label);
if (tmp.length) {
yo.push(tmp[0].id)
}
});
console.log(yo)
The code defines an array of arrays called update_var which contains two rows of objects, each with four label keys and their corresponding string values.
Then data array is defined, which contains objects with an id, other_id, and label keys with their corresponding string values.
A new array is created by using the flat() method on the update_var array, which flattens the nested arrays into a single array.
An empty array called yo is declared.
The code then iterates through each element of the flattened arrayFlat using forEach() loop and inside the loop it filters the data array by matching the label key of each object with the label key of the current element in arrayFlat.
If there is a match, it pushes the value of the id key of the first element of the filtered array to the yo array.
Finally, the yo array is logged to the console.
This code essentially filters an array of objects (data) based on a list of labels from a nested array (update_var). Then it outputs the corresponding id values.
答案3
得分: 1
你可以使用Array#filter
、Array#map
和Array#includes
方法如下所示:
const
update_vars = [
{ label: 'val 1' },
{ label: 'val 2' },
{ label: 'val 3' },
{ label: 'val 4' }
],
response = {
data: [
{
id: '112',
other_id: '352009093',
label: 'val 1'
},
{
id: '113',
other_id: '3520012',
label: 'val 2'
},
{
id: '152',
other_id: '234578',
label: 'val 13'
}
]
},
uv_labels = update_vars.map(v => v.label),
output = response.data
.filter(({label}) => uv_labels.includes(label))
.map(({id}) => id);
console.log( output );
英文:
You can use Array#filter
, Array#map
and Array#includes
methods as follows:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const
update_vars = [
{ label: 'val 1' },
{ label: 'val 2' },
{ label: 'val 3' },
{ label: 'val 4' }
],
response = {
data: [
{
id: '112',
other_id: '352009093',
label: 'val 1'
},
{
id: '113',
other_id: '3520012',
label: 'val 2'
},
{
id: '152',
other_id: '234578',
label: 'val 13'
}
]
},
uv_labels = update_vars.map(v => v.label),
output = response.data
.filter(({label}) => uv_labels.includes(label))
.map(({id}) => id);
console.log( output );
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论