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
            }
        }
    }
}
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64

2 Answers2

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));
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • If I need to encrypt the value for each key and change it into –  Sep 10 '20 at 10:25
  • 1
    @DeepanshuDemla run an encryption function on each value before your return it: `... : [key, encryptionFunction(val)]` – Nick Parsons Sep 10 '20 at 10:27
  • I have edited my question as well, can you help me with that? –  Sep 10 '20 at 10:29
  • I just need that object values to be encrypted and not that array to be converted into an object –  Sep 10 '20 at 10:30
  • 1
    @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 – Nick Parsons Sep 10 '20 at 10:37
  • you did it right, But now I just need to change the object into an encrypted form. I don't have an array as Input in this case. You can again check the question. –  Sep 10 '20 at 10:46
  • if possible just help me out with this one last time. Thanks in advance. –  Sep 10 '20 at 10:49
  • 1
    @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](https://stackoverflow.com/revisions/63810162/5) – Nick Parsons Sep 10 '20 at 10:49
  • I already did that but someone closed it saying it's not focused https://stackoverflow.com/questions/63827163/convert-object-values-to-a-encrypted-form –  Sep 10 '20 at 10:54
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/221258/discussion-between-nick-parsons-and-deepanshu-demla). – Nick Parsons Sep 10 '20 at 10:55
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));
Guilhermevrs
  • 2,094
  • 15
  • 15