2

My data:

{
  "rows": [
    {
      "id": 3,
      "code": "airtel121",
      "position": "manager",
      "salary": "25000",
      "login": {
        "id": 4,
        "username": "sameer",
        "firstName": "Mohamed",
        "lastName": "Sameer",
        "code": "airtel121",
      }
    },
    {
      "id": 7,
      "code": "airtel121",
      "position": null,
      "salary": null,
      "login": {
        "id": 8,
        "username": "annamalai",
        "firstName": "Anna",
        "lastName": "malai",
        "code": "airtel121",
      }
    }
  ]
}

My expected outcome:

{
  "rows": [
    {
      "id": 4,
      "username": "sameer",
      "firstName": "Mohamed",
      "lastName": "Sameer",
      "code": "airtel121",
      "staffs": [
        {
          "id": 3,
          "code": "airtel121",
          "position": "manager",
          "salary": "25000",
        }
      ]
    },
    {
      "id": 8,
      "username": "annamalai",
      "firstName": "Anna",
      "lastName": "malai",
      "code": "airtel121",
      "staffs": [
        {
          "id": 7,
          "code": "airtel121",
          "position": null",
          "salary": null",
        }
      ]
    }
  ]
}

I tried, but only i am getting first object, check my fiddle:

http://jsbin.com/qaqehakuwi/edit?js,output

Is this possible to loop using for loop or it can be done by lodash?

Check my above jsbin link for code.

I am using ES6 way of code in my project, so i used spread operator.

4
  • Didn't you already ask this same question here: stackoverflow.com/questions/52496180/… Commented Sep 26, 2018 at 15:14
  • yes but i got problem of object looping, so only again i am asking, this one is different problem. Commented Sep 26, 2018 at 15:15
  • I think then that you should unaccept that last answer and see if you can get a response Commented Sep 26, 2018 at 15:16
  • Possible duplicate of How to exchange the format of arrays? - javascript Commented Sep 26, 2018 at 15:35

1 Answer 1

2

You can use map to create the rows array of the new object from the rows array of the old one:

let newObj = {
  rows: oldObj.rows.map(row => {                                     // map the rows of the old object into the rows of the new object
    let { login, ...rest } = row;                                    // for each object/row get the login object as 'login' and the rest of the props as 'rest'
    return { ...login, staffs: [rest] };                             // return a new object that has the props of 'login' and an additional prop 'staffs' which is an array containing 'rest'
  })
};

Example:

let oldObj = {"rows":[{"id":3,"code":"airtel121","position":"manager","salary":"25000","login":{"id":4,"username":"sameer","firstName":"Mohamed","lastName":"Sameer","code":"airtel121"}},{"id":7,"code":"airtel121","position":null,"salary":null,"login":{"id":8,"username":"annamalai","firstName":"Anna","lastName":"malai","code":"airtel121"}}]};

let newObj = {
  rows: oldObj.rows.map(row => {
    let { login, ...rest } = row;
    return { ...login, staffs: [rest] };
  })
};

console.log(newObj);

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

2 Comments

is rest is ES6?
@MohamedSameer You're welcome! BTW, The first line is objects destructuring assignment and it's an ES6 feature. The second line is a spread syntax over objects and it's a ES2018 feature. If the second line causes problems, you can use Object.assign instead like so: return Object.assign({}, login, { staffs: [rest] });.

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.