0

i am creating a rest API and i get data from one of my request but i want to parse them

i try to create a foreach loop but a get in every line the same result there is a sample of my data

 result = [
 {
 "id": 1,
 "UserID": 2,
 "idFriend": 3,
 "status": "pending",
 "UserId": 2,
 "User": {
 "id": 2,
 "email": "[email protected]",
 "username": "tetar",
 "isAdmin": false,
 "isOut": false,
 "bio": null,
 }
 },
 {
 "id": 2,
 "UserID": 2,
 "idFriend": 1,
 "status": "pending",
 "UserId": 2,
 "User": {
 "id": 2,
 "email": "[email protected]",
 "username": "tetar",
 "isAdmin": false,
 "isOut": false,
 "bio": null,
 }
 },
]

and i want to get my data modifed like

`result = [{
 "id"= 1,
 "User": {
 "id": 2,
 "email": "[email protected]",
 "username": "tetar"
 }
 },
 {
 "id"= 2,
 "User": {
 "id": 2,
 "email": "[email protected]",
 "username": "tetar"
 }
 }
 }]`

do you know how can i do it ? thx a lot

asked Apr 16, 2018 at 21:38
3
  • Where's your code? Stackoverflow isn't a free code writing service....or a "how to" tutorial service. You are expected to show your attempts to solve issues yourself and people help you fix your code Commented Apr 16, 2018 at 21:48
  • i just past 2 hour to deal with this issue and in 3 min in Stackoverflow someone give me the solution. i think the purpose of this web site is to help each other. Commented Apr 16, 2018 at 21:53
  • No...that is not how it is supposed to work. Take some time to read help center Commented Apr 16, 2018 at 22:08

2 Answers 2

2

Just a mapping function.

const newResult = [];
result.forEach((r) => {
 newResult.push({
 id: r.id,
 user: {
 id: r.User.id,
 email: r.User.email,
 username: r.User.username
 }
 });
});
console.log(newResult);

I am pretty sure there are better cleaner and more performant ways to do this. I recommend using lodash map function:

https://lodash.com/docs/4.17.5#map

answered Apr 16, 2018 at 21:43
Sign up to request clarification or add additional context in comments.

Comments

1

You can use Array.prototype.map:

result = [
 {
 "id": 1,
 "UserID": 2,
 "idFriend": 3,
 "status": "pending",
 "UserId": 2,
 "User": {
 "id": 2,
 "email": "[email protected]",
 "username": "tetar",
 "isAdmin": false,
 "isOut": false,
 "bio": null,
 }
 },
 {
 "id": 2,
 "UserID": 2,
 "idFriend": 1,
 "status": "pending",
 "UserId": 2,
 "User": {
 "id": 2,
 "email": "[email protected]",
 "username": "tetar",
 "isAdmin": false,
 "isOut": false,
 "bio": null,
 }
 },
]
let out = result.map(el=>{return {id: el.id, User:{id: el.User.id, email: el.User.email, username:el.User.username}}});
console.log(out);

answered Apr 16, 2018 at 21:44

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.