-3

I have array of object with same key name. I want to pass each address key inside that object as parameter to my api. How I can do this ? I get response like this { '0': { address: 'abcd' }, '1': { address: 'xyz' } }.

const getDataWithAddress = async ({ ...address }) => {
        // console.log(address);
        const res = await transport.post(`${api}/get`, address);
        console.log(res);
        // return res
}

getDataWithAddress([{ address: 'abcd'}, { address: 'xyz' }])
KushagraMish
  • 165
  • 2
  • 2
  • 6
  • What is "my API", exactly? Do you mean a RESTful web-service, or a JavaScript library? What is `transport`, exactly? – Dai Jan 19 '22 at 14:32
  • We don't know what format your API expects the data to be presented in, so we can't tell you how to encode the data in that format. – Quentin Jan 19 '22 at 14:33
  • yeah its rest api – KushagraMish Jan 19 '22 at 14:33
  • Most likely duplicate of: [From an array of objects, extract value of a property as array](https://stackoverflow.com/q/19590865) – VLAZ Jan 19 '22 at 14:38

1 Answers1

0

Based on the code in your question, it looks like you want to make n requests for n address objects. However, it looks like your request function only takes one address at a time.

const getDataWithAddress = async (address) => {
    const res = await transport.post(`${api}/get`, address);
    return res;
}

const myAddresses = [{ address: "abc" }, { address: "xyz" }];

// Wait for an aggregate of Promises.
const results = await Promise.all(
    // Turn each address object into a REST API request for that object.
    myAddresses.map(addObj => getDataWithAddress(addObj.address))
);

console.log(results);
// Whatever your expected API results are.
// [{ AddressObject: { ... } }, { AddressObject: { ... } }]
Zulfe
  • 820
  • 7
  • 25