1

I'm using promise.AllSettled to fetch data from 2 / 3 endpoints and result looks like this

Simplified results array

const results = [
  {
    value: {
      payload: {
      email: "[email protected]",
      first_name: "",
      last_name: "",
      pk: 183,
      username: "faker",
      }
    },
    error: true,
  },
  {
    value: {
      payload: {
      avatar: "https://cdn.zeplin.io/faker.png",
      email: "[email protected]",
      id: 183,
      is_admin: false,
      username: "faker",
      }
    },
    error: false  
  }
]

I would like to get flat object that contains something like this, but generated dynamically (the number of the payloads may vary)

const desiredObj = Object.assign({}, results[0].value.payload, results[1].value.payload)
/*
  avatar: "https://cdn.zeplin.io/faker.png",
  email: "[email protected]",
  first_name: "",
  id: 183,
  is_admin: false,
  last_name: "",
  pk: 183,
  username: "faker"

*/
2
  • 3
    something like Object.assign({}, ...results.map(x => x.value.payload))? Commented Sep 14, 2020 at 18:34
  • Yes, thank you very much, it will be great if you could write an answer I'll gladly mark it as answer :) Commented Sep 14, 2020 at 18:38

1 Answer 1

1

const results = [
  {
    value: {
      payload: {
        email: "[email protected]",
        first_name: "",
        last_name: "",
        pk: 183,
        username: "faker"
      }
    }, error: true
  },
  {
    value: {
      payload: {
        avatar: "https://cdn.zeplin.io/faker.png",
        email: "[email protected]",
        id: 183,
        is_admin: false,
        username: "faker",
      }
    },
    error: false
  }
];

console.log(
  Object.assign({}, ...results.map(x => x.value.payload))
);

Lux

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

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.