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
Push is a function Array.push()
this.data[index].push('banana');
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
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"]
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');
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.
Related
JS noob here. I want to store a list of option for a dropdown in an array. I want a key to be associated with every value something like this
var newArray = [
{
key: "key",
val:({value:"val", label:"label"}, {value:"val",label:"label"})
}
]
The code above returns undefined when I try to read val. What is the solution? Thanks
var newArray = [
{
key: "key",
val:[{value:"val", label:"label"}, {value:"val",label:"label"}]
}]
The only thing i changed were parentheses () into [], because it's the thing that declares an array. Now, if you want to read the val key, you need to do this stuff.
You have an array named "newArray". It only has 1 element (which is also the first).
Now lets get the first element of the array by newArray[0]. Now you have accessed the object inside the array. Now you can read the the value by newArray[0].val. And now you have entered a new array, which elements you can read with newArray[0].val[0] and newArray[0].val[1]
I am trying to create a multi dimensional array programmatically. But when i push an object into the array it gets pushed into all indexes in the array. Why is this happening
Here's a simple demo
let postTest = new Array(4).fill([]);
postTest[0].push({key: 'value', anotherKey: 'value'});
console.log(postTest);
Use Array.from instead, Array.prototype.fill copies the reference of object on all places so any change at one place will reflect at all places
let postTest = Array.from({length: 4}, ()=> []);
postTest[0].push({ key: 'value', anotherKey: 'value' });
console.log(postTest);
As mentioned in the fill() method docs:
value
Value to fill the array with. (Note all elements in the array will be this exact value.)
You can fix this issue, by assigning the empty array to individual index using .map() so that there no reference issue like:
let postTest = new Array(4).fill().map(x=>[]);
postTest[0].push({key: 'value', anotherKey: 'value'});
console.log(postTest);
You can also, try:
let postTest = [...Array(4)].map(x=>[]);
postTest[0].push({key: 'value', anotherKey: 'value'});
console.log(postTest);
I have an object with key value pairs inside an array:
var data = [
{
"errorCode":100,
"message":{},
"name":"InternetGatewayDevice.LANDevice.1.Hosts.HostNumberOfEntries",
"value":"2"
}
];
I want to get the value of "value" key in the object. ie, the output should be "2".
I tried this:
console.log(data[value]);
console.log(data.value);
Both logging "undefined". I saw similar questions in SO itself. But, I couldn't figure out a solution for my problem.
You can use the map property of the array. Never try to get the value by hardcoding the index value, as mentioned in the above answers, Which might get you in trouble. For your case the below code will works.
data.map(x => x.value)
You are trying to get the value from the first element of the array. ie, data[0]. This will work:
console.log(data[0].value);
If you have multiple elements in the array, use JavaScript map function or some other function like forEach to iterate through the arrays.
data.map(x => console.log(x.value));
data.forEach(x => console.log(x.value));
data is Array you need get first element in Array and then get Value property from Object,
var data = [{
"ErrorCode":100,
"Message":{},
"Name":"InternetGatewayDevice.LANDevice.1.Hosts.HostNumberOfEntries",
"Value":"2"
}];
console.log(data[0].Value);
Try this...
Actually Here Data is an array of object so you first need to access that object and then you can access Value of that object.
var data = [
{
"ErrorCode":100,
"Message":{},
"Name":"InternetGatewayDevice.LANDevice.1.Hosts.HostNumberOfEntries",
"Value":"2"
}
];
alert(data[0].Value);
what you are trying to read is an object which an element of an array, so you should first fetch the element of array by specifying its index like
data[0] and then read a property of the fetched object, i.e. .value,
so the complete syntax would be data[0].value
Hope it helps !
I have an array called 'inputs' that I want to push some values to.
I am wondering if this is the correct way to do so:
inputs.push(name: $('#uploaderName').name(), value: $('#uploaderName').val());
#uploaderName is the ID of a form field with the value I want pushed.
FYI, the 'inputs' array is created like this:
var inputs = data.context.find(':input').not(':button');
I have also tried this with no luck:
inputs.push({name: $('#uploaderName').attr("name"), value: $('#uploaderName').val()});
To explain a bit more, at the end of this script it calls:
data.formData = inputs.serializeArray();
and then that data is passed off to a PHP script. I need to be able to add a few values to 'inputs' before inputs.serializeArray() is called.
For jQuery collections, you can include additional Elements with .add().
inputs = inputs.add('#uploaderName');
jQuery collections aren't actually Arrays. They're just array-like, meaning they have a length and numeric properties, but they won't have Array methods like .push().
You are storing the object, so use {...} to make the object first, and store it into inputs array, like
inputs.push({name: $('#uploaderName').name(), value: $('#uploaderName').val()});
//----------^----------------------------------------------------------------^
Or
var myObj = {name: $('#uploaderName').name(), value: $('#uploaderName').val()};
inputs.push(myObj);
i am trying to get a value from a key stored on a string variable proyNombre, but whenever i call it via the common method "myAssociativeArray.MyKey", it gets the variable 'proyNombre' as the key, instead of getting its value and passing it as a key.
proyectos.each(function(index){
var proyNombre = this.value;
if(!(proyNombre in myArray)){ // whenever the variable is undefined, define it
myArray[proyNombre] = horas[index].value-0 + minutos[index].value/60;
}
else{
console.log(myArray.proyNombre); //This doesnt work, it tries to give me the value for the key 'proyNombre' instead of looking for the proyNombre variable
console.log(myArray.this.value); //doesnt work either
}
});
Try:
console.log(myArray[proyNombre]);
myArray is actually an object in javascript. You can access object properties with object.propertyName or, object['propertyName']. If your variable proyNombre contained the name of a property (which it does) you can use the second form, like I did above. object.proyNombre is invalid - proyNombre is a variable. You can't do for example:
var myObject = {};
myObject.test = 'test string';
var s = 'test';
console.log(myObject.s); // wrong!!
but you could then do:
console.log(myObject.test);
console.log(myObject['test']);
console.log(myObject[s]);
You need to use the same syntax you used to set the value:
console.log(myArray[proyNombre]);
Simply access the value with myArray[proyNombre].
You're doing it right in the assignment: myArray[proyNombre]. You can use the same method to retrieve the variable.
If you change:
console.log(myArray.proyNombre);
console.log(myArray.this.value);
to
console.log(myArray[proyNombre]);
console.log(myArray[this.value]);
You should get the same value (the value for the key represented by the variable proyNombre) logged twice.
It's true that Javascript doesn't have associative arrays but objects in Javascript can be treated like associative arrays when accessing their members.