Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用Lodash修改数组中的关键点_Javascript_Lodash - Fatal编程技术网

Javascript 使用Lodash修改数组中的关键点

Javascript 使用Lodash修改数组中的关键点,javascript,lodash,Javascript,Lodash,我试图用lodash修改一组对象,但有点费劲。我想使用下面的数组并更改对象中的键 这是我的源阵列: [ { "id": "AD", "name": "Andorra", "currency": "EUR", "timezone": "Europe/Andorra", "links": [ { "rel": "self",

我试图用lodash修改一组对象,但有点费劲。我想使用下面的数组并更改对象中的键

这是我的源阵列:

[
    {
        "id": "AD",
        "name": "Andorra",
        "currency": "EUR",
        "timezone": "Europe/Andorra",
        "links": [
            {
                "rel": "self",
                "href": "http://localhost:8000/api/countries/AD"
            },
            {
                "rel": "country.currency",
                "href": "http://localhost:8000/api/currencies/EUR"
            }
        ]
    },
    {
        "id": "AE",
        "name": "United Arab Emirates",
        "currency": "AED",
        "timezone": "Asia/Dubai",
        "links": [
            {
                "rel": "self",
                "href": "http://localhost:8000/api/countries/AE"
            },
            {
                "rel": "country.currency",
                "href": "http://localhost:8000/api/currencies/AED"
            }
        ]
    },
    ...
    ]
我希望这能导致:

[
    {
        "value": "AD",
        "title": "Andorra",
        "currency": "EUR",
        "timezone": "Europe/Andorra",
        "links": [
            {
                "rel": "self",
                "href": "http://localhost:8000/api/countries/AD"
            },
            {
                "rel": "country.currency",
                "href": "http://localhost:8000/api/currencies/EUR"
            }
        ]
    },
    {
        "value": "AE",
        "title": "United Arab Emirates",
        "currency": "AED",
        "timezone": "Asia/Dubai",
        "links": [
            {
                "rel": "self",
                "href": "http://localhost:8000/api/countries/AE"
            },
            {
                "rel": "country.currency",
                "href": "http://localhost:8000/api/currencies/AED"
            }
        ]
    },
    ...
    ]
请注意,前两个键已重命名。我知道我可以使用
\ mapKeys
修改键,但是我不确定如何以最佳方式迭代数组

任何建议都将不胜感激。

使用(或lodash)迭代,并使用(或)生成带有更改键的新对象

“指定”将使用新关键帧与原始对象中的相应值以及不包含要使用替换的关键帧的原始对象进行融合

var数据=[{“id”:“AD”,“name”:“andror”,“currency”:“EUR”,“timezone”:“Europe/andror”,“links”:[{“rel”:“self”,“href”:http://localhost:8000/api/countries/AD},{“rel”:“country.currency”,“href”:http://localhost:8000/api/currencies/EUR“}]},{“id”:“AE”,“姓名”:“阿拉伯联合酋长国”,“货币”:“AED”,“时区”:“亚洲/迪拜”,“链接”:[{“rel”:“self”,“href”:”http://localhost:8000/api/countries/AE},{“rel”:“country.currency”,“href”:http://localhost:8000/api/currencies/AED"}]}];
var结果=data.map(函数(o){
返回Object.assign({
值:o.id,
标题:o.name
},省略(o,'id','name');
});
console.log(结果);
使用(或lodash)迭代,并使用(或)生成带有更改键的新对象

“指定”将使用新关键帧与原始对象中的相应值以及不包含要使用替换的关键帧的原始对象进行融合

var数据=[{“id”:“AD”,“name”:“andror”,“currency”:“EUR”,“timezone”:“Europe/andror”,“links”:[{“rel”:“self”,“href”:http://localhost:8000/api/countries/AD},{“rel”:“country.currency”,“href”:http://localhost:8000/api/currencies/EUR“}]},{“id”:“AE”,“姓名”:“阿拉伯联合酋长国”,“货币”:“AED”,“时区”:“亚洲/迪拜”,“链接”:[{“rel”:“self”,”href:“http://localhost:8000/api/countries/AE},{“rel”:“country.currency”,“href”:http://localhost:8000/api/currencies/AED"}]}];
var结果=data.map(函数(o){
返回Object.assign({
值:o.id,
标题:o.name
},省略(o,'id','name');
});
console.log(结果);

您可以使用
..map
创建新对象,根据条件更改键并返回新对象

var obj=[{
“id”:“AD”,
“名称”:“安道尔”,
“货币”:“欧元”,
“时区”:“欧洲/安道尔”,
“链接”:[{
“rel”:“self”,
“href”:”http://localhost:8000/api/countries/AD"
}, {
“rel”:“国家货币”,
“href”:”http://localhost:8000/api/currencies/EUR"
}]
}, {
“id”:“AE”,
“名称”:“阿拉伯联合酋长国”,
“货币”:“AED”,
“时区”:“亚洲/迪拜”,
“链接”:[{
“rel”:“self”,
“href”:”http://localhost:8000/api/countries/AE"
}, {
“rel”:“国家货币”,
“href”:”http://localhost:8000/api/currencies/AED"
}]
}];
var updatedObj=uj.map(对象,函数(currObj,索引){
var newObj={};
Object.key(currObj).forEach(函数(key){
如果(键=='id'){
newObj.value=currObj[key];
}else if(key=='name'){
newObj.name=currObj[key];
}否则{
newObj[key]=currObj[key];
}
});
返回newObj;
});
console.log(updatedbj);

您可以使用
..map
创建新对象,根据条件更改键并返回新对象

var obj=[{
“id”:“AD”,
“名称”:“安道尔”,
“货币”:“欧元”,
“时区”:“欧洲/安道尔”,
“链接”:[{
“rel”:“self”,
“href”:”http://localhost:8000/api/countries/AD"
}, {
“rel”:“国家货币”,
“href”:”http://localhost:8000/api/currencies/EUR"
}]
}, {
“id”:“AE”,
“名称”:“阿拉伯联合酋长国”,
“货币”:“AED”,
“时区”:“亚洲/迪拜”,
“链接”:[{
“rel”:“self”,
“href”:”http://localhost:8000/api/countries/AE"
}, {
“rel”:“国家货币”,
“href”:”http://localhost:8000/api/currencies/AED"
}]
}];
var updatedObj=uj.map(对象,函数(currObj,索引){
var newObj={};
Object.key(currObj).forEach(函数(key){
如果(键=='id'){
newObj.value=currObj[key];
}else if(key=='name'){
newObj.name=currObj[key];
}否则{
newObj[key]=currObj[key];
}
});
返回newObj;
});
console.log(updatedbj);

我不希望更改原始数据,因此我会这样做:

const thing = [{id:"AD",name:"Andorra",currency:"EUR",timezone:"Europe/Andorra",links:[{rel:"self",href:"http://localhost:8000/api/countries/AD"},{rel:"country.currency",href:"http://localhost:8000/api/currencies/EUR"}]},{id:"AE",name:"United Arab Emirates",currency:"AED",timezone:"Asia/Dubai",links:[{rel:"self",href:"http://localhost:8000/api/countries/AE"},{rel:"country.currency",href:"http://localhost:8000/api/currencies/AED"}]}];

const newThing = [];

thing.map( item => {
    newThing.push(
        _.mapKeys( item, ( value, key ) => {
            let newKey = key;
            if( key === 'id' ) {
                newKey = 'value';
            }

            if( key === 'name' ) {
                newKey = 'title';
            }

            return newKey;
        })
    )
});

console.log( newThing );

我不希望更改原始数据,因此我会这样做:

const thing = [{id:"AD",name:"Andorra",currency:"EUR",timezone:"Europe/Andorra",links:[{rel:"self",href:"http://localhost:8000/api/countries/AD"},{rel:"country.currency",href:"http://localhost:8000/api/currencies/EUR"}]},{id:"AE",name:"United Arab Emirates",currency:"AED",timezone:"Asia/Dubai",links:[{rel:"self",href:"http://localhost:8000/api/countries/AE"},{rel:"country.currency",href:"http://localhost:8000/api/currencies/AED"}]}];

const newThing = [];

thing.map( item => {
    newThing.push(
        _.mapKeys( item, ( value, key ) => {
            let newKey = key;
            if( key === 'id' ) {
                newKey = 'value';
            }

            if( key === 'name' ) {
                newKey = 'title';
            }

            return newKey;
        })
    )
});

console.log( newThing );

使用
map
进行迭代并在每次迭代中运行
mapKeys
?非常好的建议,谢谢大家使用
map
进行迭代并在每次迭代中运行
mapKeys
?非常好的建议,谢谢大家为什么定义一个新列表(newThing)?
map
自己返回一个新列表为什么定义一个新列表(newThing)?
map
返回新列表本身