2

I would like to make an API call with the data formatted like this.

[{"Id":10, “FolderID”:22},{"Id":12, “FolderID”:22}]

I have these different data sources below: (using redux)

const FolderId = indexes.currentFolder.id;

This is a constant value

const documentsId = indexes.copyDocuments;

this is the format of 8, 9 , 10

I tried to implement it by looping through the documents array with a foreach.

The only way i know how to attach the data is with an array but this creates a new array of each loop with is not what i want.

const data = [];

documentsId.forEach(function(doc){
  data.push(`"Id":${doc},"FolderID":${FolderId}`)
})

Does anyone know a way of doing this? Thanks.

7
  • maybe try something along the lines of: var tmp = {}; tmp.Id = doc.Id; tmp.FolderId = doc.FolderId; data.push(tmp); Commented Jul 16, 2018 at 16:54
  • what you want to do is quite simple. But can you provide more details on your arrays. Console what is documentsId . Commented Jul 16, 2018 at 16:56
  • I'm not sure how you're passing the FolderId in the following data.push("Id":${doc},"FolderID":${FolderId}) . If you're pushing an object to your array you can use data.push({"Id":`${doc}`,"FolderID":`${FolderId}`}. Commented Jul 16, 2018 at 17:01
  • Array 0 : 50 1 : 49 length : 2. Commented Jul 16, 2018 at 17:08
  • when i try an use an object instead of an array it says its undefined Commented Jul 16, 2018 at 17:09

2 Answers 2

2

The things you're passing into the data array are strings, when what you want is objects. Instead of:

data.push(`"Id":${doc},"FolderID":${FolderId}`)

....do:

data.push({ Id: doc, FolderID: FolderId })

...and data will be an array of objects instead of strings.

I'd also recommend using map like @JoeWarner suggested as a more succinct way to build the Array.

const data = documentsId.map(doc => ({ Id: doc, FolderID: FolderId }))
Sign up to request clarification or add additional context in comments.

Comments

2

What i believe you're looking for is the map prototype

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

what map does is return a new array from an orignal array so in your example we have an array of

[8, 9, 10]

we map over this the same way we'd use forEach it gets passed the item then the index

const newArray = [8, 9, 10].map(function (item, index) {
  return [item, index]
})
// [[8, 0], [9, 1], [10, 2]]

the map prototype returns a NEW array so no need to copy your array as to not mutate it this makes the map prototype immutable

const documentsId = [8, 9, 10]

/* however you get your folderId */
const folderId = 22;

const mappedToObjectArray = documentsId.map(id => ({id, folderId}))


/*
  If you're used to es5 this might look more familiar 

  function formatData() {
    return documentsId.map(function (doc) {
      return {
        id: doc,
        folderId: folderId
      }
    })
  }
*/

console.log(mappedToObjectArray)

Please let me know if i've misunderstood or you're confused about anything happy to help.

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.