javascript 数组Map函数无法在简单示例中推送对象

ljsrvy3e  于 2023-03-11  发布在  Java
关注(0)|答案(6)|浏览(131)

我在数组中获取对象集的地方获取数据(带有国家和州键沿着值)。
现在,我需要以这样一种方式格式化数据,即得到一个新数组(以countryName作为键,以state-in array作为值)。
我正试着做那件事,但没有成功。有人能帮我吗?

var data = [
    {
        "country": "United States",
        "states": [
            "Alabama",
            "Alaska",
            "Arizona",
        ]
    },
    {
        "country": "Canada",
        "states": [
            "Ontario",
            "Saskatchewan",
            "Alberta",
        ]
    },
    {
        "country": "Australia",
        "states": [
            "Australian Capital Territory",
            "Jervis Bay Territory",
            "New South Wales",
            "Queensland",
        ]
    }
];

newData = []; 
data.map(item => newData.push({item.country: item.states});
console.log(newData);
q3qa4bjr

q3qa4bjr1#

这是因为带点的对象键不是有效语法。您可以通过用[] Package 键来解决此问题。此外,.map()方法创建一个新数组,其中填充了对调用数组中的每个元素调用所提供函数的结果。因此,您应该在此处使用.forEach()方法,如下所示:

data.forEach(item => newData.push({[item.country]: item.states}));

x一个一个一个一个x一个一个二个x
或者,您可以使用.map()方法,在.map()回调中简单地返回对象,并将其存储在newData中,如下所示:

newData = data.map(item => ({[item.country]: item.states}));
var data=[{country:"United States",states:["Alabama","Alaska","Arizona"]},{country:"Canada",states:["Ontario","Saskatchewan","Alberta"]},{country:"Australia",states:["Australian Capital Territory","Jervis Bay Territory","New South Wales","Queensland"]}]

var newData = data.map(item => ({[item.country]: item.states}));
console.log(newData);
.as-console-wrapper { max-height: 100% !important; top: 0; }
6yt4nkrj

6yt4nkrj2#

正确使用map是在map方法中返回值,这在你的代码中是缺失的。除此之外,使用反结构来进一步简化。

const newData = data.map(({country, states}) => ({[country]: states}));
var data = [
    {
        "country": "United States",
        "states": [
            "Alabama",
            "Alaska",
            "Arizona",
        ]
    },
    {
        "country": "Canada",
        "states": [
            "Ontario",
            "Saskatchewan",
            "Alberta",
        ]
    },
    {
        "country": "Australia",
        "states": [
            "Australian Capital Territory",
            "Jervis Bay Territory",
            "New South Wales",
            "Queensland",
        ]
    }
];

const newData = data.map(({country, states}) => ({[country]: states}));
console.log(newData);
wgx48brx

wgx48brx3#

你可以试试这个:
它很符合你的要求。

data.forEach(item => {
    const obj = new Object();
    obj[item.country] = item.states;
    newData.push(obj)
});

这里我们使用new关键字来声明我们的Object,并将对象推入Array中。另外,由于country是一个变量,因此我们使用[]表示法而不是dot表示法。

zwghvu4y

zwghvu4y4#

像这样试试。

var data = [
    {
        "country": "United States",
        "states": [
            "Alabama",
            "Alaska",
            "Arizona",
        ]
    },
    {
        "country": "Canada",
        "states": [
            "Ontario",
            "Saskatchewan",
            "Alberta",
        ]
    },
    {
        "country": "Australia",
        "states": [
            "Australian Capital Territory",
            "Jervis Bay Territory",
            "New South Wales",
            "Queensland",
        ]
    }
];

let newData = data.map(item => { return { [item.country] : [...item.states]}})
console.log(newData);
chy5wohz

chy5wohz5#

let arr = []
array.forEach(element => {
  arr.push({key:element.country, value: element.state})
}

**如果您使用map,请返回,但我认为您在这种情况下使用forEach

tjvv9vkg

tjvv9vkg6#

请使用for循环,对于异步等待条件,Map不能正常工作。

相关问题