按属性前缀提取对象

时间:2020-03-23 05:07:14

标签: javascript ecmascript-6

我试图遍历对象中的巨大属性列表,但未能提取具有相同前缀的属性,我不能使用对象delete函数,因为该列表很大,我的错误在哪里?

const a = {
  obj_abc: true,
  obj_def: false,
  hello_123: true,
  hello_456: 'another value'
};

let b = {};

for(k in a){
    const [key] = k.split('_');
    if(key === 'hello') {
      b = {...b[key], [key]:a[k]} //the problem is here, it gave me only hello_456: 'another value'

    }
}

console.log(b);

5 个答案:

答案 0 :(得分:0)

尝试使用括号符号

const a = {
  obj_abc: true,
  obj_def: false,
  hello_123: true,
  hello_456: 'another value'
};

let b = {};

for (k in a) {
  const [key] = k.split('_');
  if (key === 'hello') {
    b[k] = a[k];
  }
}

console.log(b);

使用startsWith()

const a = {
  obj_abc: true,
  obj_def: false,
  hello_123: true,
  hello_456: 'another value'
};

let b = {};

for (k in a) {
  if (k.startsWith('hello_')) {
    b[k] = a[k];
  }
}

console.log(b);

答案 1 :(得分:0)

对于keyhello,您的hello_123均为hello_456,因此它覆盖了hello键的旧条目。您需要唯一的密钥。例如下面。

const a = {
  obj_abc: true,
  obj_def: false,
  hello_123: true,
  hello_456: 'another value'
};

let b = {};

for(k in a){
    const [key] = k.split('_');
    if(key === 'hello') {
    //key is hello for both hello_123 and hello_456, hence its overriding
      b[k] = a[k] //the problem is here, it gave me only hello_456: 'another value'

    }
}

console.log(b);

答案 2 :(得分:0)

尝试一下

const a = {
  obj_abc: 123,
  obj_def: 456,
  hello_123: 123,
  hello_456: 456
};

// filter out the keys that start with hello
var keys = Object.keys(a).filter(function(k) {
  return k.indexOf("hello") === 0;
});

//to convert an array of filtered keys into an object of key-value pairs
var res = keys.reduce(function(matched, k) {
  matched[k] = a[k];
  return matched;
}, {});

console.log(res);

答案 3 :(得分:0)

您可以使用条目,减少以获得简洁的代码。可以同时创建所有键的映射,以后可以很好地提取。参见示例2。

// Example 1
const a = {
    obj_abc: true,
    obj_def: false,
    hello_123: true,
    hello_456: 'another value'
};

const result = Object.entries(a).reduce((map, [key, value]) => {
    if (key.indexOf("hello_") === 0) map[key] = value
    return map
}, {})

console.log(result);

// To collect all in once
// Example 2
const result2 = Object.entries(a).reduce((map, [key, value]) => {
    const [k] = key.split("_")
    if(!map[k]) map[k] = {}
    map[k][key] = value
    return map
}, {})

console.log(result2); // { obj: { obj_abc: true, obj_def: false }, hello: { hello_123: true, hello_456: 'another value' } }
console.log(result2["hello"]); // { hello_123: true, hello_456: 'another value' }
console.log(result2["obj"]); // { obj_abc: true, obj_def: false }

答案 4 :(得分:0)

请找到我的答案。

const a = {
  obj_abc: true,
  obj_def: false,
  hello_123: true,
  hello_456: "another value"
};

let b = {};

for (key in a) {
  let [text] = key.split("_");

  if (!(text in b)) {
    b[text] = { [key]: a[key] };
  }
 else {
    Object.assign(b[text], { [key]: a[key] });
  }
}

console.log(b);

输出

{
  "obj": {
    "obj_abc": true,
    "obj_def": false
  },
  "hello": {
    "hello_123": true,
    "hello_456": "another value"
  }
}
相关问题