I have a multi dimensional array like this:
var firstElement = $(this).first();
fielddata = {
number: index,
attributes: [
{ 'label_text': firstElement.text(),
'label_width': firstElement.width(),
'label_height': firstElement.height(),
'label_color': firstElement.css('color')
}
]
}
How can I change one of the values inside the attributes part but by id? so I make 'label_text' a different value?
I do not want to use the index.
You don't have a multidimensional array. You have an object; one of its properties is an array of objects.
In this case, it looks like you want to update label_text in the first (and only) object in the attributes list. If that's correct:
fielddata.attributes[0].label_text = 'whatever';
Will work, as would:
fielddata.attributes[0]['label_text'] = 'whatever';
Related
I have array, created from json:
var array = [{"name":"name1","group":"group1","id":"123", ...},
{"name":"name2","group":"group2","id":"456", ...},
{"name":"name3","group":"group1","id":"789", ...}];
After I get another array:
var array1 = [{"name":"name1","group":"group1","id":"123", ...},
{"name":"name4","group":"group1","id":"987", ...}]
I need to push items from second array into first, but how can I check if first array contains objects from second array?
Each object in array contain more property and some of them are created dynamically so I can't check for example by indexOf(). All solutions that I found works only with simple objects like Int. It will be great if I could check by property "id" for example.
Use find first
var newObj = {"name":"name2","group":"group2","id":"456"};
var value = array.find( s => s.id == newObj.id );
Now push if the value is not found
if ( !value )
{
array.push( newObj )
}
(More generic)you can do this one line using following (which will add all object which is not in array).
array.concat(array1.filter(x=>!array.find(s=>s.id==x.id)));
var array = [{"name":"name1","group":"group1","id":"123"},
{"name":"name2","group":"group2","id":"456" },
{"name":"name3","group":"group1","id":"789"}];
var array1 = [{"name":"name1","group":"group1","id":"123"},
{"name":"name4","group":"group1","id":"987"}];
array=array.concat(array1.filter(x=>!array.find(s=>s.id==x.id)));
console.log(array);
Can any one please help me how to add objects to another array of Objects
myArray = [
{
"A" :{
values
},
"B" :{
values
},
"C":{
values
}
}
]
another Object:
{
"D":{
values
},
"E":{
values
}
}
I want to add next objects like D and E to My Array of First Object.
it shuold be like this
[
{
"A":{},
"B":{},
"C":{},
"D":{},
"E":{}
}
]
Cna you help me any one how to add this objects
Thanks in Advance
According to my understanding you have one array of object i.e.
myArray = [{"A" :{},"B" :{},"C":{}}]
and you want to add some property in that object so just use
myArray[0].D={};
myArray[0].E={};
console.log(myArray[0]);
And if You want to add more objects in Array use push method
var obj={"A1" :{},"B1" :{},"C1":{}}
if you want to add in myArray then use
myArray.push(obj);
yours array of object will be
myArray = [{"A" :{},"B" :{},"C":{}},
{"A1" :{},"B1" :{},"C1":{}}]
Hope it clears yours doubt.
try something like this
myarray=[{"A":"e1"},{"B":"e2"},{"C":"e4"}];
var obj={"D":"e5"};
myarray.push(obj);
alert(myarray[3].D)
will alert e5
DEMO
Update :
myarray=[{"A":"e1"},{"B":"e2"},{"C":"e4"}];
var obj={"D":"e6","E":"e9"};
myarray.push(obj);
alert(myarray[3].F)
will alert e9
I want to create an array like this in JQuery:
id=1, name="pepe"
How can I do this? I have done this but not working
arr[idx]["id"].push( $(id).text());
arr[idx]["name"].push( $(name).text());
to later access to the id of the array like this:
(arr[0].id)
If your goal is to have an array where each entry in the array has id and name attributes, here's how:
Declare a variable:
var arr;
Create the array:
arr = [];
Add an entry to it which is an object with those properties:
arr.push({
id: $(id).text(), // I assume the `id` in `$(id)` is just placeholder for something
name: $(name).text() // Similarly the `name` in `$(name)`
});
In the above, provided $(id).text() returns "1" and $(name).text() returns "Pepe", you'll end up with an array with one entry, where that one entry is an object with id=1 and name=Pepe.
Then you can access (say) the first of those:
console.log(arr[0].id); // Shows the `id` property of the first object in the array
arr.push({id:1, name:"pepe"});
arr[idx].id = $(id).text();
arr[idx].name = $(name).text();
I am trying to create a list of "items" in a canvas game. For example, an array named list. Each element must contain the information about each item. First element will contain something different. I will remove first one with 'shift()' command. Like :
list.shift();
list[0]['name']
list[0]['id']
list[0]['x']
list[0]['y']
list[1]['name']
list[1]['id']
list[1]['x']
list[1]['y']
but i don't know how to define something like this. normally i define arrays like
{"name" : xx, "id" : 5 ... }
but this works like :
list['name']
list['id']
use:
var list = [];
list[0] = {name: 'xx', id: 0, /*etc*/};
list[1] = {name: 'yy', id: 1, /*etc*/};
it creates an array of objects. You can use it like this:
var first = list.shift();
first.name; //=> xx
//or
var first = list[0];
first.name; //=> xx
Note: using {...} (Object literal) creates an Object, not an Array. An array can be created using an Array literal: [...]. Although an object is sometimes said to be an Associative Array, it is not an Array object, so things like {...}.shift() will not work for Objects.
There are no associative arrays in javascript.
so for instance , when you do
var _array = []
_array["field1"] ="value";
you are actually adding a property to the _array object .
_array.field1 = value <=> _array["field1"] ="value";
so if you want to create a collection of objects , do
var collection =[];
var myObject = {"field1":"value1"};
collection.push(myObject);
I have an object like this:
object["key1"] = "text1"
object["key2"] = "text2"
object["key3"] = "text1"
object["key4"] = "text3"
How can I give out (e.g. alert) the elements with the same values (text1, text2 and so on)?
In the above example it should be object["key1"] and object["key2"].
Thanks
You could "invert" your object (properties become values, values become properties):
var byValue = {};
for (var prop in object) {
if (!(object[prop] in byValue)) {
byValue[object[prop]] = [];
}
byValue[object[prop]].push(prop);
}
This should yield this structure:
{
'text1': ['key1', 'key3'],
'text2': ['key2'],
'text3': ['key4']
}
Then, you can detect those values that had duplicate keys:
for (var value in byValue) {
if (byValue[value].length > 1) {
alert(byValue[value].join(', '));
}
}
I have sorted the array and then considered that you would want to alert,or do any
functionality, only once for every repeated element.
WARNING: Sorting can get heavy with the size of the array
http://jsfiddle.net/SPQJ7/
The above fiddle is already setup and working with multiple reapeated elements
I updated my script
http://jsfiddle.net/HerrSerker/LAnRt/
This does not check for identity in complex values, just for equality (see foo example)