Home > Mobile >  Search array of nested objects with dynamic parameters
Search array of nested objects with dynamic parameters

Time:09-21

I have an array with objects. I'm trying to recursively search through them and create new array with modified key's.

The array:

    let response = {
    users: [
        {
            user_id: 1661871600,
            details: {
                height: 150,
                age: 33,
                work: 1015,
                school: 10,
                degree: 933
            },
            work: [
                {
                    id: 500,
                    main: 'Doctor',
                    description: 'Ofto',
                    icon: '10d',
                },
            ],
            look: {
                all: 100,
            },
            transport: {
                speed: 0.62,
                max_speed: 349,
            },
            visibility: 10000,
            hair: {
                type: 0.26,
            },
            date: '2022-08-30 15:00:00',
        },
        {
            user_id: 324235256,
            details: {
                height: 110,
                age: 25,
                work: 101,
                school: 110,
                degree: 553
            },
            work: [
                {
                    id: 500,
                    main: 'Software',
                    description: 'Programmer',
                    icon: '11d',
                },
            ],
            look: {
                all: 2,
            },
            transport: {
                speed: 1.2,
                max_speed: 49,
            },
            visibility: 221,
            hair: {
                type: 5.6,
            },
            date: '2021-01-30 12:00:00',
        },
        {
            user_id: 12353743,
            details: {
                height: 10,
                age: 75,
                work: 1,
                school: 114,
                degree: 3
            },
            work: [
                {
                    id: 500,
                    main: 'Pension',
                    description: 'Architect',
                    icon: '1d',
                },
            ],
            look: {
                all: 6,
            },
            transport: {
                speed: 3.2,
                max_speed: 29,
            },
            visibility: 21,
            hair: {
                type: 1.6,
            },
            date: '2020-01-30 12:00:00',
        },
    ],
};

I've tried using JSON.Stringify but I keep getting not desired result.

    function dynamic(obj, ...keyToFind) {
    const foundObj = {};
    const foundObjArray: any[] = [];
    keyToFind.forEach((key) => {
        JSON.stringify(obj, (_, nestedValue) => {
            if (nestedValue && nestedValue[a]) {
                foundObj[key] = nestedValue[a];
            }
            return nestedValue;
        });
        foundObjArray.push(foundObj);
    });
    return foundObjArray;
}

I'm calling this function with N number of parameters:

dynamic(response, 'age', 'work', 'main', 'description', 'type', 'date');

Once it is called I'm trying to construct a new array of objects who will look like:

let foundObjArray = [
   {
     'age': 33,
     'work': 1015,
     'main': 'Doctor',
     'description': 'Ofto',
     'type': 5.6,
     'date': '2022-08-30 15:00:00'
   },
   {
     'age': 25,
     'work': 101,
     'main': 'Software',
     'description': 'Programmer',
     'type': 0.26,
     'date': '2021-01-30 12:00:00'
   },
   .......
]

CodePudding user response:

let response = {
    users: [
        {
            user_id: 1661871600,
            details: {
                height: 150,
                age: 33,
                speed_min: 296.76,
                speed_max: 297.87,
                work: 1015,
                school: 10,
                degree: 933
            },
            work: [
                {
                    id: 500,
                    main: 'Doctor',
                    description: 'Ofto',
                    icon: '10d',
                },
            ],
            look: {
                all: 100,
            },
            transport: {
                speed: 0.62,
                max_speed: 349,
            },
            visibility: 10000,
            hair: {
                type: 0.26,
            },
            date: '2022-08-30 15:00:00',
        },
        {
            user_id: 324235256,
            details: {
                height: 110,
                age: 25,
                speed_min: 96.76,
                speed_max: 327.87,
                work: 101,
                school: 110,
                degree: 553
            },
            work: [
                {
                    id: 500,
                    main: 'Software',
                    description: 'Programmer',
                    icon: '11d',
                },
            ],
            look: {
                all: 2,
            },
            transport: {
                speed: 1.2,
                max_speed: 49,
            },
            visibility: 221,
            hair: {
                type: 5.6,
            },
            date: '2021-01-30 12:00:00',
        },
        {
            user_id: 12353743,
            details: {
                height: 10,
                age: 75,
                speed_min: 16.76,
                speed_max: 27.87,
                work: 1,
                school: 114,
                degree: 3
            },
            work: [
                {
                    id: 500,
                    main: 'Pension',
                    description: 'Architect',
                    icon: '1d',
                },
            ],
            look: {
                all: 6,
            },
            transport: {
                speed: 3.2,
                max_speed: 29,
            },
            visibility: 21,
            hair: {
                type: 1.6,
            },
            date: '2020-01-30 12:00:00',
        },
    ],
};

function getMatchedKey(arr, key) {
    let matched = arr.filter(x => key.endsWith(x));
    if(matched.length > 0) {
        return matched[0];
    }
}

function getLimitedData(data, requiredKey = []) {
    var result = {};
    function recurse(cur, prop) {
        if (Object(cur) !== cur) {
            let matchedKey = getMatchedKey(requiredKey, prop);
            matchedKey != undefined && (result[matchedKey] = cur);
        } else if (Array.isArray(cur)) {
            for (var i = 0, l = cur.length; i < l; i  )
                recurse(cur[i], prop   "["   i   "]");
            if (l == 0) {
                let matchedKey = getMatchedKey(requiredKey, prop);
                matchedKey != undefined && (result[matchedKey] = []);
            }
        } else {
            var isEmpty = true;
            for (var p in cur) {
                isEmpty = false;
                recurse(cur[p], prop ? prop   "."   p : p);
            }
            if (isEmpty && prop) {
                let matchedKey = getMatchedKey(requiredKey, prop);
                matchedKey != undefined && (result[matchedKey] = {});
            }
        }
    }
    recurse(data, "");
    return result;
}

let keys = ['age', 'work', 'main', 'description', 'type', 'date'];
let output = response.users.map(a => getLimitedData(a, keys));
console.log(output);

  • Related