使用 Lodash 修改数组中的键

zag*_*010 5 javascript lodash

我正在尝试用 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"
            }
        ]
    },
    ...
    ]
Run Code Online (Sandbox Code Playgroud)

我希望这会导致:

[
    {
        "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"
            }
        ]
    },
    ...
    ]
Run Code Online (Sandbox Code Playgroud)

请注意,前两个键已重命名。我知道我可以用来_.mapKeys修改键,但是我不确定如何以最佳方式迭代数组来做到这一点。

任何建议将不胜感激。

Ori*_*ori 5

使用Array#map(或 lodash 的_.map())进行迭代,并使用Object#assign(或_.assign())使用更改键生成一个新对象。

分配 会将带有新键的对象与原始对象中的相应值以及不带要使用 替换的键的原始对象融合_.omit()

var data = [{"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"}]}];

var result = data.map(function(o) {
  return Object.assign({
    value: o.id,
    title: o.name
  }, _.omit(o, 'id', 'name'));
});

console.log(result);
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

另一种选择是使用_.mapKeys()更改键名称:

var data = [{"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"}]}];

var keys = { id: 'value', name: 'title' };

var result = data.map(function(o) {
  return _.mapKeys(o, function(v, k) {
    return k in keys ? keys[k] : k;
  });
});

console.log(result);
Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
Run Code Online (Sandbox Code Playgroud)


Dom*_*nik 2

我不想更改原始数据,所以这就是我要做的:

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 );
Run Code Online (Sandbox Code Playgroud)