0

I have an array of objects. I want to pick few values from my array of objects and send it in my request params on for my api that i am calling on componentDidMount. My Array-

const myArray = [
  {
    id:'73',
    name:'ABC',
    age: '20',
  },
  {
    id:'74',
    name:'XYZ',
    age: '21',
  },
  {
    id:'75',
    name:'PQR',
    age: '22',
  },
  {
    id:'76',
    name:'TUV',
    age: '23',
  }
] 


useEffect(
    () => {
      const newData = myArray.map(list => {
      // Logic to add few more keys to existing array
      return newData; // new array with few additional properties
      });

      let data= newData.map(({name, age}) => ({name,age}));
      const reqParams = {
        userData: {
          data: [
            {
              data,
            },
          ],
        },
      };
      getUserData(reqParams); // API call
    [],
  );

To get values, I am doing like this-

let result = myArray.map(({name, age}) => name,age)

This will return me an array so, I am not sure if this is the right way to do it. trying to understand how can i pass the values to my request params.

1
  • Can you show us the logic you used to add more keys to the newData arrray? Commented May 24, 2020 at 16:39

3 Answers 3

1

Here is the sample code to achieve the desired output in a single loop 🚀

const myArray = [{
    id: '73',
    name: 'ABC',
    age: '20',
}, {
    id: '74',
    name: 'XYZ',
    age: '21',
}, {
    id: '75',
    name: 'PQR',
    age: '22',
}, {
    id: '76',
    name: 'TUV',
    age: '23',
}]


const newArr = myArray.map((d,index) => {
    // return new obj structure with subset of myArray and extra values
    
    const extraData = {
        company: 'foo', // adding company & city [read/get it from anywhere]
        city:'bar',
        sampleIndex: index+1
    }

    const newObj = {
        name: d.name, // not adding id, age
        ...extraData
    }

    return newObj
})

console.log("newArr ", newArr)

const reqParams = {
  userData: {
    data: newArr
  },
};

console.log("reqParams ", reqParams)

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

Comments

0

You can create new object in map callback by wrapping it in ({}) braces

let data= myArray.map(({name, age}) => ({name,age}));

const reqParams = {
        userData: {
          data
        },
      };

1 Comment

I have updated my Question above. I am handling like you have suggested but i think probably i am doing some mistake in using map method. My newData gives me an array with all values as undefined and hence, i am not able to see name and age in my request params.
0

Please try to write the newData calculation as follows if not already:

const newData = myArray.map(list => {
  return {
    ...list,
    key: value // additional property
  }
});

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.