0

I have a json and i want to split each key value pair to new json object.

Input:

 tokenData = [{
  "firstname" : "Priya",
  "from": "21-09-2001",
  "to": "22-08-2001",
  "address": "zczxczxczx"
}]

Expected Result:

  tokenItems = [
    {"firstname" : "Priya"},
    {"from": "21-09-2001"},
    {"to": "22-08-2001"},
    {"address": "zczxczxczx"}
  ];

2 Answers 2

1

You can achieve to that result without lodash.

Use Object.entries, get the key/values and then use map function to create your desired structure of objects.

const tokenData = [{
   "firstname" : "Priya",
   "from": "21-09-2001",
   "to": "22-08-2001",
   "address": "zczxczxczx"
}];

const newTokenData = Object.entries(tokenData[0])
                           .map(([key, val]) => ({ [key]: val }));

console.log(newTokenData);

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

Comments

0

Why use lodash, Simply you can get this using plan javascript, you need to iterate over the key/value pair for each object like this -

    const tokenData = [{
       "firstname" : "Priya",
       "from": "21-09-2001",
       "to": "22-08-2001",
       "address": "zczxczxczx"
    }];
    
    let newArr = []
    for (let a in tokenData[0]){
    	newArr.push({[a]: tokenData[0][a]});
    } 
    console.log(newArr);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.