I tried to loop through a huge list of properties in object, but failed to extract properties that has the same prefix, I can't use object delete
function because the list is huge, where is my mistake?
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);
Try using bracket notation
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);
Using 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);
Your key
is hello
for both hello_123
and hello_456
, hence its overriding the old entry for hello
key. you need unique keys. eg below.
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);
Try this
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);
You can use entries, reduce for clean code. Same time you can create map of all key, later good to extract. See the example 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 }
Please find my answer.
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);
OUTPUT
{
"obj": {
"obj_abc": true,
"obj_def": false
},
"hello": {
"hello_123": true,
"hello_456": "another value"
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.