javascript按键过滤,并将键的值映射到另一个对象的新对象键

问题描述 投票:1回答:5

我通过基于一些允许的键过滤键来构造一个对象:

const configObject = { 
  key1: 'value1', key2: 'value2', key3: 'value3', key4: 'value4'
};
const allowed = ['key1', 'key3'];

const filtered = Object.keys(configObject)
  .filter(key => allowed.includes(key))
  .reduce((obj, key) => {
    obj[key] = configObject[key];
    return obj;
  }, {});

console.log(filtered) // { key1: 'value1', key3: 'value3' }

但是,我想从具有漂亮键名的键/值配对对象获取新对象的键:

const allowed = [{'key1': 'keyOne'}, {'key3': 'keyThree'}];

我怎样才能实现这一目标以获得以下结果:

 { keyOne: 'value1', keyThree: 'value3' }
javascript arrays object ecmascript-6
5个回答
2
投票

您可以获取keys对象的条目并映射新属性。

var configObject = { key1: 'value1', key2: 'value2', key3: 'value3', key4: 'value4' },
    allowed = [{'key1': 'keyOne'}, {'key3': 'keyThree'}],
    filtered = Object.assign(
        ...allowed.map(o => 
            Object.assign(...Object.entries(o).map(([o, n]) =>
                ({ [n]: configObject[o] })))
        )
    );

console.log(filtered);

随着减少

var configObject = { key1: 'value1', key2: 'value2', key3: 'value3', key4: 'value4' },
    allowed = [{'key1': 'keyOne'}, {'key3': 'keyThree'}],
    filtered = allowed.reduce((r, o) => {
        Object.entries(o).forEach(([o, n]) => r[n] = configObject[o]);
        return r;
    }, {});

console.log(filtered);

2
投票

您可以使用allowed作为字典,而不仅仅是列表:

const configObject = {
  key1: 'value1',
  key2: 'value2',
  key3: 'value3',
  key4: 'value4'
};
const allowed = {'key1': 'keyOne', 'key3': 'keyThree'};
const filtered = Object.keys(configObject)
  .filter(key => Object.keys(allowed).includes(key))
  .reduce((obj, key) => {
    obj[allowed[key]] = configObject[key];
    return obj;
  }, {});

console.log(filtered) // "keyOne": "value1", "keyThree": "value3" }

1
投票

您可以使用Array.reduce()迭代allowed数组,并使用configObject上存在的键构建新对象:

const configObject = { key1: 'value1', key2: 'value2', key3: 'value3', key4: 'value4' };
const allowed = [{'key1': 'keyOne'}, {'key3': 'keyThree'}, 'key2', 'key5']; // I've added key2 as string, and the non existing key5

const result = allowed.reduce((r, o) => {
  // get the key and alias from an object or string
  const [key, alias] = typeof o === 'object' ? Object.entries(o)[0] : [o, o];
  
  if(key in configObject) { // if key exists in configObject
    r[alias] = configObject[key]; // assign it to r;
  }
  
  return r;
}, {});

console.log(result);

1
投票

只需更改对象的允许数组并使用相同的逻辑

const configObject = { 
  key1: 'value1', 
  key2: 'value2', 
  key3: 'value3', 
  key4: 'value4'
};

const allowed = {
  key1: 'keyOne',
  key3: 'keyThree'
};
const allowedKeys = Object.keys(allowed)

const filtered = Object.keys(configObject)
  .filter(key => allowedKeys.includes(key))
  .reduce((obj, key) => {
    obj[allowed[key]] = configObject[key];
    return obj;
  }, {});

console.log(filtered) 

1
投票

如果您只想从特定数字中获取漂亮的名称,请使用正则表达式:

/([a-z]+)(\d+)/i

这适用于像keywhatever1111key1anotherkey2这样的任何键,等等。

var prettyOnes = {
  '1': 'One',
  '2': 'Two',
  '3': 'Three',
  '11': 'Eleven'
};

const configObject = { 
  key1: 'value1', key2: 'value2', key3: 'value3', whatever11: 'value11'
};
const allowed = ['key1', 'key3', 'whatever11'];

const filtered = Object.keys(configObject)
  .filter(key => allowed.includes(key))
  .reduce((obj, key) => {
    var [_, pre, pretty] = key.match(/([a-z]+)(\d+)/i);
    obj[`${pre}${prettyOnes[pretty]}`] = configObject[key];
    return obj;
  }, {});

console.log(filtered);
.as-console-wrapper { max-height: 100% !important; top: 0; }
© www.soinside.com 2019 - 2024. All rights reserved.