地图未按预期从axios返回数组。

huangapple go评论75阅读模式
英文:

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 `[&#39;112&#39;, &#39;113&#39;]`

</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:&quot;val 1&quot;},{label:&quot;val 2&quot;},{label:&quot;val 3&quot;},{label:&quot;val 4&quot;}],response={data:[{id:&quot;112&quot;,other_id:&quot;352009093&quot;,label:&quot;val 1&quot;},{id:&quot;113&quot;,other_id:&quot;3520012&quot;,label:&quot;val 2&quot;},{id:&quot;152&quot;,other_id:&quot;234578&quot;,label:&quot;val 13&quot;}]};
const labels = new Set(update_vars.map(o =&gt; o.label));
const res = response.data.filter(({label}) =&gt; labels.has(label)).map(o =&gt; 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: &#39;val 1&#39; },
  { label: &#39;val 2&#39; },
  { label: &#39;val 3&#39; },
  { label: &#39;val 4&#39; }
],
[
  { label: &#39;val 5&#39; },
  { label: &#39;val 6&#39; },
  { label: &#39;val 7&#39; }
]];

var data = [{
  id: &#39;112&#39;,
  other_id: &#39;352009093&#39;,
  label: &#39;val 1&#39;
},
{
  id: &#39;113&#39;,
  other_id: &#39;3520012&#39;,
  label: &#39;val 2&#39;
},
{
  id: &#39;152&#39;,
  other_id: &#39;234578&#39;,
  label: &#39;val 13&#39;
}]

const arrayFlat = update_var.flat()
const yo = []


arrayFlat.forEach(el =&gt; {
  const tmp = data.filter(e =&gt; 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#filterArray#mapArray#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: &#39;val 1&#39; },
{ label: &#39;val 2&#39; },
{ label: &#39;val 3&#39; },
{ label: &#39;val 4&#39; }
],
response = {
data: [
{
id: &#39;112&#39;,
other_id: &#39;352009093&#39;,
label: &#39;val 1&#39;
},
{
id: &#39;113&#39;,
other_id: &#39;3520012&#39;,
label: &#39;val 2&#39;
},
{
id: &#39;152&#39;,
other_id: &#39;234578&#39;,
label: &#39;val 13&#39;
}
]
},
uv_labels = update_vars.map(v =&gt; v.label),
output = response.data
.filter(({label}) =&gt; uv_labels.includes(label))
.map(({id}) =&gt; id);
console.log( output );

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月18日 08:11:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定