简体   繁体   English

Array.map函数不适用于Object值

[英]Array.map function isn't applying to Object value

I'm attempting to re-map an Object that comes from an API call. 我正在尝试重新映射来自API调用的Object。 The format of the response is the following: 响应的格式如下:

data: {
    foo: [{
        id: "1",
        name: "joe",
        info: "whatever"
    }, {
        id: "2",
        name: "anna",
        info: "whatever"
    },...],
    bar: [...]
}

The code I'm using to re-map the object inside each response array is: 我用来重新映射每个响应数组中的对象的代码是:

const DATA = response.data;
const entries = Object.entries(DATA);

for (const entry of entries) {
    entry[1].map(entry => ({
        id: entry["id"],
        mixed_info: entry["name"] + ", " + entry["info"]
    }));
}

When I console.log the data after this, it shows the same as the initial response, as if it completly ignored the map function. 当我在此之后控制数据时,它显示与初始响应相同的内容,就好像它完全忽略了map函数一样。

What am I doing wrong? 我究竟做错了什么? Thanks for the attention. 感谢您的关注。

map 返回一个新数组,你忽略了调用的结果。

Assign the result of the map call: 分配map调用的结果:

entry[1] = entry[1].map(...);

Array.prototype.map returns a new array - it doesn't modify the original. Array.prototype.map返回一个新数组 - 它不会修改原始数组。

You have to reassign the entry : 您必须重新分配entry

  entry[1] = entry[1].map(/*...*/);

However that will only get reflected to the array inside entries , it won't change DATA . 但是,这只会反映到entries内的数组,它不会改变DATA To change that, you have to either turn the key value pairs back into an object at the end: 要更改它,您必须将键值对转回到最后的对象中:

  DATA = Object.fromEntries(entries);

Or you have to reassign the DATA properties while iterating: 或者您必须在迭代时重新分配DATA属性:

   DATA[ entry[0] ] = entry[1].map(/*...*/);

I'd do: 我会做:

  const { data } = response;

  for(const [key, value] of Object.entries(data)) {
     data[key] = value.map(/*...*/);
  }
    let result = {};
    for (const entry of entries) {
        result[entry[0]] = entry[1].map(entry => ({
            id: entry.id,
            mixed_info: `${entry["name"]  }, ${  entry["info"]}`,
        }));
    }

'result' contains exact remapped object. 'result'包含精确重映射的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 索引无法在React array.Map函数中按预期工作 - index isn't working as expected in React array.Map function getStaticPaths array.map 在部署到 Vercel 时不是 function - getStaticPaths array.map isn't a function when deploying to Vercel 我如何更新状态以使用 array.map 函数更新数组内对象的值 - how do i update state in react for updating the value of an object inside an array using array.map function 有什么方法可以使用Javascript的Array.map函数返回对象+添加到对象的新键值对 - Is there any way to use Javascript's Array.map function to return the object + new key value pair added to it 为什么 array.map() 不是 function? - Why is array.map() not a function? 通过嵌套函数传递函数,并通过array.map传递当前值 - Pass function with nested function and current value with array.map 如果键在Array.map中已经存在,则将值推送到对象 - Push value to object if key already exists during Array.map mobx:array.map()不是函数 - mobx: array.map() is not a function 如何从javascript中的array.map中的异步函数返回一个值? - How to return a value from a async function in array.map in javascript? 即使未输入映射循环,React也会在array.map上崩溃 - React crashes on array.map even though the map loop isn't being entered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM