1

I have an object called featureSet.

Inside of featureSet there are many items, including an array called features, which contains other array, attributes.

I can add a new array inside of featureSet.features.attributes by doing the following within a for loop

featureSet.features[i].attributes.NEWITEM= [NEWITEM_ARRAY];

And when I use console.log(featureSet), I can see that the items are there.

When I use var test = JSON.stringify(featureSet), however, only the original featureSet is returned.

How can I circumvent this so that when I call JSON.stringify, the new items are there as well?

Thank you in advance.

4
  • 1
    Can you post a minimal reproducible example that reproduces the problem? Sounds like you need to wait for the asynchronous population to complete Commented Oct 18, 2018 at 0:20
  • 1
    attributes is an array but you add new property NEWITEM to it as if it is an object? Commented Oct 18, 2018 at 0:48
  • Yea need to see the code it could be a number of things..
    – Pari Baker
    Commented Oct 18, 2018 at 1:04
  • @TarekEssam - I think you're correct. Attributes is an array of arrays. NEW ITEM would be an array as well. What would be the correct way to do this? Commented Oct 18, 2018 at 1:45

3 Answers 3

1

I guess what you do is close to:

let arr = []
console.log(arr) // []
arr.push(1)
console.log(arr) // [1]
arr.abc = 2
console.log(arr.abc) // 2
arr.push(3)
console.log(arr) // [1, 3]
console.log(JSON.stringify(arr)) // '[1, 3]'
console.log(arr.abc) // 2

JSON.stringify loops through array props with the help of Symbol.iterator. Your properties do not have positive integer indexes, that's why they are ignored. There is an example on MDN as well: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

One more example to consider (continues the one above):

arr[7] = 7
console.log(arr) // [1, 3, undefined, undefined, undefined, undefined, undefined, 7]
console.log(JSON.stringify(arr)) // "[1,3,null,null,null,null,null,7]"
1

How can I circumvent this so that when I call JSON.stringify, the new items are there as well?

You are trying to append attributes to an array, which will not work.

You can only append attributes to an object.

/* the original feature set */
const featureSet = {
	features: [
  	{feature: 'color',
    	attributes: {}}, // this is an object, not an array
    {feature: 'shape',
      attributes: []}    // this is an array as per original scenario
  ]
}

/* adding attributes to an object succeeds */
featureSet.features[0].attributes.NEWITEM = ['this', 'was', 'appended'];

/* adding attributes to an array quietly fails */
featureSet.features[1].attributes.NEWITEM = ['this', 'was', 'not', 'appended'];


const featureSetAsJSONString = JSON.stringify(featureSet)
console.log(featureSetAsJSONString) // notice that feature[0] is as expected, but [1] isn't

Hope this helps. Cheers,

0

Try this

featureSet.features[i].attributes.push({NEWITEM: [NEWITEM_ARRAY]})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.