0
[["seller" , [["id" , "1"], ["name", "test"]]], ["token", "aasfgsgd"], ["settings", [["general", false], ["store", [["trusted", true], ["socialMedia", [["fbConnected", true], ["igConnected", false]]]]]]]]

This nested array can be variable, So convert it into an Object like shown below

{
    "seller": {
    "id": 1,
    "name": "test"
    },
    "token": "aasfgsgd",
    "settings": {
        "general": false,
        "store": {
            "trusted": true,
            "socialMedia": {
                "fbConnected": true,
                "igConnected": false
            }
        }
    }
}
0

2 Answers 2

2

You can use Object.fromEntries() with a recursive function to convert all nested [[key, value], ...] pair arrays into objects themselves.

The Object.fromEntries() method is able to take an array such as:

[["key", "val2"]]

... and convert it into an object:

{
  "key": "val2"
}

However, if "val2" is an array itself, this would need to be converted into an object first. This can be done by recursively calling Object.fromEntries() on the value entries which are arrays.

See example below:

const arr = [["seller" , [["id" , 1], ["name", "test"]]], ["token", "aasfgsgd"], ["settings", [["general", false], ["store", [["trusted", true], ["socialMedia", [["fbConnected", true], ["igConnected", false]]]]]]]];

const makeObject = arr => {
  return Object.fromEntries(arr.map(
    ([key, val]) => Array.isArray(val) ? [key, makeObject(val)] : [key, val] 
  ));
}

console.log(makeObject(arr));

A more browser-friendly approach would be to use .reduce() with the spread syntax instead of Object.fromEntries():

const arr = [["seller" , [["id" , 1], ["name", "test"]]], ["token", "aasfgsgd"], ["settings", [["general", false], ["store", [["trusted", true], ["socialMedia", [["fbConnected", true], ["igConnected", false]]]]]]]];

const makeObject = arr => {
  return arr.reduce((o, [key, val]) => {
    return Object.assign(o, {[key]: Array.isArray(val) ? makeObject(val) : val});
  }, {});
}

console.log(makeObject(arr));

Sign up to request clarification or add additional context in comments.

10 Comments

If I need to encrypt the value for each key and change it into
@DeepanshuDemla run an encryption function on each value before your return it: ... : [key, encryptionFunction(val)]
I have edited my question as well, can you help me with that?
@DeepanshuDemla your question is asking for an object output with encrypted values... do you want an array or an object. I've added the way to get your desired result based on the edit you made in your question
@DeepanshuDemla Hi, I see what you want now. Although this question is somewhat related to your old question, it is still a different question. If you have a new question you can post a new question (rather than editing the old one), as this makes the two answers you've received make no sense to future readers. I've rolled back your question to the original (feel free to post a new question) - you can grab the contents of your new question from here
|
0

const array = [["seller" , [["id" , "1"], ["name", "test"]]], ["token", "aasfgsgd"], ["settings", [["general", false], ["store", [["trusted", true], ["socialMedia", [["fbConnected", true], ["igConnected", false]]]]]]]];

function objectify(arr) {
  return arr.reduce((acc, item) => {
    let builder;
    if (Array.isArray(item[0])) {
      builder = objectify(item[0])
    } else {
      const propName = item[0];
      const propValue = Array.isArray(item[1]) ? objectify(item[1]) : item[1];
      builder = {[propName]: propValue};
    }
    return {
       ...acc,
       ...builder
    }
  }, {});
}

console.log(objectify(array));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.