how to push array object name javascript 1

how to push array object name javascript

function getKeyValue(object) {
    return Object.keys(object).reduce(function (result, key) {
        return result.concat(
            object[key] && typeof object[key] === 'object' ?
            getKeyValue(object[key]) :
            [[key, object[key]]]
    }, []);

var data = { id: 23, name: "Jacob", link: { rel: "self", link: "", }, company: { data: { id: 1, ref: 324 } } };


Here is what the above code is Doing:
1. We are using reduce() to iterate over the keys of the object.
2. We are checking if the value of the key is an object. If it is, we are calling the same function recursively.
3. If the value is not an object, we are returning the key and the value as an array.
4. We are using concat() to merge the result of the recursive call with the result of the current iteration.

Similar Posts