0

i have pushed data to an array like this

this.data.push{'data': 'type',
  'value': 'apple'}

and i want to append the value of that particular array's value object. I tried to do it like this

this.data[index].value.push = 'banana';

but it doesn't work?? I want to replace the value

5 Answers 5

4

Push is a function Array.push()

this.data[index].push('banana');
Sign up to request clarification or add additional context in comments.

2 Comments

I want to replace only the value object of that particular index not all the data
Than you should do this this way: this.data[index].value = "your new value"
2

Adding items to arrays work like this in javascript:

this.data.push({'data': 'type', 'value': 'apple'});

However, given that your data is an object, you don't need to use push:

this.data[index].value = 'banana';

You can access a value from a javascript object directly.

Given that you have used string keys, you will probably have to do the following:

this.data[index]['value'] = 'banana';

Look at this for more info: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

3 Comments

I tried it. It's not working. it's giving Cannot set property 'value' of undefined this error
So the problem is that this.data is not getting the correct thing. You should do console.log(this.data); and tell me what you see.
Ok so what that either means is that this.data is not defined or you're putting an index that is out of range of the array. You'll either need to use this.data[0] or look at how this.data is defined. I suggest you make a new variable like so: var data = {'data': 'type', 'value': 'apple'}; then try doing what everyone has suggested on this new variable instead.
2

i have pushed data to an array like this

this.data.push{'data': 'type', 'value': 'apple'}

No! That does not work without getting a syntax error.

You could use

this.data.push({'data': 'type', 'value': 'apple'});

for inserting a new object at the end of the array data.

For changing a specific objects's property, you need to assign the new content, like

this.data[index].value = 'banana';

This takes an element and the property and assigns the new value 'banana' to it.

Please have a look to properties accessors for objects, like

object.property
object["property"]

Comments

1

make it

this.data[index].value = ['banana'];

since you need to replace the value of value attribute with an array.

If this needs to be repeated again then

Array.isArray(this.data[index].value) ? (this.data[index].value = ['banana']) : this.data[index].value.push('banana');

Comments

1

push takes a parameter of object, you must have pass this parameter in braces

this.data.push({'data': 'type',  'value': 'apple'})

to change value you can directly change field value

this.data[index].value = 'banana';

if you want to keep multiple values in value then make value an array as well.

1 Comment

you want extra field to insert or want to change the value?

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.