简体   繁体   中英

javascript add missing keys in an object inside array

I have the following array of objects.

{
    "students": {
        "fees": {
            "others": [
                {
                    "Student Name": "Ashy",
                    "Term1": "$1,000.00",
                    "Term2": "$1,000.00",
                    "Total": "$2,000.00",
                },
                {
                    "Student Name": "Test",
                    "Term1": "$1,000.00",
                    "Total": "$1,000.00",
                }
            ],
        }
    }
}

Here, second object does not have the key Term2 . I want to have same keys in both the objects inside array. The output should be like below:

{
    "students": {
        "fees": {
            "others": [
                {
                    "Student Name": "Ashy",
                    "Term1": "$1,000.00",
                    "Term2": "$1,000.00",
                    "Total": "$2,000.00",
                },
                {
                    "Student Name": "Test",
                    "Term1": "$1,000.00",
                    "Term2": "",
                    "Total": "$1,000.00",
                }
            ],
        }
    }
}

Could anyone please let me know how i can achieve this in javascript ?

Thanks

Here i create a list of all keys of all object keys within others array. And overwrite the original others array with a new one creates based on the list of keys.

 const data = { "students": { "fees": { "others": [ { "Student Name": "Ashy", "Term1": "$1,000.00", "Term2": "$1,000.00", "Total": "$2,000.00", }, { "Student Name": "Test", "Term1": "$1,000.00", "Total": "$1,000.00", } ], } } }; const allKeys = [...new Set(data.students.fees.others.flatMap(Object.keys))]; data.students.fees.others = data.students.fees.others.map((otherentry) => { return Object.fromEntries(allKeys.map((key) => [key, otherentry[key] || ""])); }); console.log(data)

Use a dictionary of keys to check against. map over the array of objects, and then use that dictionary to decide if a new property needs to be added to the object.

 const data = { "students": { "fees": { "others": [{ "Student Name": "Ashy", "Term1": "$1,000.00", "Term2": "$1,000.00", "Total": "$2,000.00", }, { "Student Name": "Test", "Term1": "$1,000.00", "Total": "$1,000.00", } ], } } } const dict = ['Student Name', 'Term1', 'Term2', 'Total']; const out = data.students.fees.others.map(obj => { // Grab the object keys const keys = Object.keys(obj); // Iterate over the dictionary and if a // key is missing add it to the object for (let key of dict) { if (!keys.includes(key)) obj[key] = ''; } return obj; }); console.log(out);

When a missing key needed to be added, it's not necessarily in the same order as your example, but that doesn't affect the functionality or quality of data.

 let data = { "students": { "fees": { "others": [{ "Student Name": "Ashy", "Term1": "$1,000.00", "Term2": "$1,000.00", "Total": "$2,000.00", }, { "Student Name": "Test", "Term1": "$1,000.00", "Total": "$1,000.00", } ], } } } let keys = [] data.students.fees.others.forEach(o => { if (Object.keys(o).length > keys.length) keys = Object.keys(o); }) data.students.fees.others = data.students.fees.others.map(m => { if (Object.keys(m).length !== keys.length) { keys.forEach(k => { if (!m.hasOwnProperty(k)) m[k] = ""; }) } return m }) console.log(data)

First findout the all keys to have in object. Then, Use map to build new array and fill the missing keys with empty values.

 const data = { "students": { "fees": { "others": [{ "Student Name": "Ashy", "Term1": "$1,000.00", "Term2": "$1,000.00", "Total": "$2,000.00", }, { "Student Name": "Test", "Term1": "$1,000.00", "Total": "$1,000.00", } ], } } } const { students: { fees: { others } } } = data; const keys = others.reduce( (acc, curr) => (Object.keys(curr).forEach((key) => acc.add(key)), acc), new Set() ); const output = others.map((item) => [...keys].reduce((acc, key) => ((acc[key] = item[key] ?? ""), acc), {}) ); console.log(output)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM