最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to extract key-value pairs from a object, on a particular string filter? - Stack Overflow

programmeradmin3浏览0评论
const obj = {
"pi_diagram": null,
"painting": null,
"heat_treatment": null,
"welding_procedure": null,
"inspection_test": null,
"pipecl_hadoop": null,
"pipecl": null,
"ludo_min_hadoop": null,
"ludo_min": 4,
"ludo_normal_hadoop": null,
"ludo_normal": 6,
"ludo_max_hadoop": null,
"ludo_max": null,
"ludo_test": null,
"ludo_mech_min": null,
"ludo_mech_max": null,
"ludo_unit": "barg",
"temp_min_hadoop": null

}

I am having this object , how to extract key value pair having '_hadoop' appended in key ??

const obj = {
"pi_diagram": null,
"painting": null,
"heat_treatment": null,
"welding_procedure": null,
"inspection_test": null,
"pipecl_hadoop": null,
"pipecl": null,
"ludo_min_hadoop": null,
"ludo_min": 4,
"ludo_normal_hadoop": null,
"ludo_normal": 6,
"ludo_max_hadoop": null,
"ludo_max": null,
"ludo_test": null,
"ludo_mech_min": null,
"ludo_mech_max": null,
"ludo_unit": "barg",
"temp_min_hadoop": null

}

I am having this object , how to extract key value pair having '_hadoop' appended in key ??

Share Improve this question asked Jul 7, 2020 at 8:48 njainnjain 1573 silver badges8 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 4

you can refer this Q : JavaScript: filter() for Objects

for your Q it would be as :

   const obj = {
      "pi_diagram": null,
      "painting": null,
      "heat_treatment": null,
      "welding_procedure": null,
      "inspection_test": null,
      "pipecl_hadoop": null,
      "pipecl": null,
      "ludo_min_hadoop": null,
      "ludo_min": 4,
      "ludo_normal_hadoop": null,
      "ludo_normal": 6,
      "ludo_max_hadoop": null,
      "ludo_max": null,
      "ludo_test": null,
      "ludo_mech_min": null,
      "ludo_mech_max": null,
      "ludo_unit": "barg",
      "temp_min_hadoop": null
    };
    const filteredByKey = Object.fromEntries(Object.entries(obj).filter(([key, value]) => key.includes('_hadoop')))

console.log(filteredByKey);

If you want to strictly check name ends with _hadoop and name doesn't contains _hadoop then you need to use regex like /_hadoop$/.test(propName).

Use Object.keys(obj) to get array of all keys of obj and then filter with /_hadoop$/.test(x) so it will return array of key ends with _hadoop.

Then use reduce to build your new object.

Check the result below.

const obj = {
  "pi_hadoop_diagram": null,
  "painting": null,
  "heat_treatment": null,
  "welding_procedure": null,
  "inspection_test": null,
  "pipecl_hadoop": null,
  "pipecl": null,
  "ludo_min_hadoop": null,
  "ludo_min": 4,
  "ludo_normal_hadoop": null,
  "ludo_normal": 6,
  "ludo_max_hadoop": null,
  "ludo_max": null,
  "ludo_test": null,
  "ludo_mech_min": null,
  "ludo_mech_max": null,
  "ludo_unit": "barg",
  "temp_min_hadoop": null
};

let result = Object.keys(obj)
  .filter(x => /_hadoop$/.test(x))
  .reduce((a, x) => (a[x] = obj[x], a), {});

console.log(result);

const obj = {
  "pi_diagram": null,
  "painting": null,
  "heat_treatment": null,
  "welding_procedure": null,
  "inspection_test": null,
  "pipecl_hadoop": null,
  "pipecl": null,
  "ludo_min_hadoop": null,
  "ludo_min": 4,
  "ludo_normal_hadoop": null,
  "ludo_normal": 6,
  "ludo_max_hadoop": null,
  "ludo_max": null,
  "ludo_test": null,
  "ludo_mech_min": null,
  "ludo_mech_max": null,
  "ludo_unit": "barg",
  "temp_min_hadoop": null
}

let result = {}
for (const [key, value] of Object.entries(obj)) {
  if (key.includes('_hadoop')) {
    result[key] = value
  }
}
console.log(result)

发布评论

评论列表(0)

  1. 暂无评论