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
Related
If I have an array of objects, for example:
var cars = [{name:"Geronimo", color:"red"},{name:"Ronaldo",color:"green"}]
Is there a simple way Im not seeing to select all the "color" attributes? So that I would get:
"red", "green"
So say something like (INVENTED):
console.log(cars[selectingallobjects].name)
Thanks in advance!
There isn't a ready way of doing that, you need to iterate to get the values.
You can use .map(), an Array method that creates a new array based on the values returned from the function.
It can be done in a single line.
See below:
var cars = [{name:"Geronimo", color:"red"},{name:"Ronaldo",color:"green"}]
let result = cars.map(car => car.color)
console.log(result)
you can use map to do the following, it loops for every object in the array.
var cars = [{name:"Geronimo", color:"red"},{name:"Ronaldo",color:"green"}]
cars.map((eachObject)=>{
console.log(eachObject.color);
});
Using for-loop and take only color property
var cars = [{name:"Geronimo", color:"red"},{name:"Ronaldo",color:"green"}]
var carNames = []
for(car of cars){
carNames.push(car['color'])
}
console.log(carNames)// [ "red", "green" ]
use map which is method creates a new array:
var carNames = cars.map(car=>car.color)
I have an array of objects, let's say array[object,object....] . I want to add a new property to each object inside an array.
Below i have mentioned and existing array and the resulted one which i want.
Existing array :
array[ {"name":"Siddhesh mishra","add":"hjhjjdjkhjibf",}
{"name":"Brijesh mishra","add":"jkfhgfbrfhiurf"} ]
I want this array :
array[ {"name":"Siddhesh mishra","add":"hjhjjdjkhjibf","mobile":"95937338373"}
{"name":"Brijesh mishra","add":"jkfhgfbrfhiurf","mobile":"78984983498"} ]
How can i do this ?
You can do like this. Let there be an array of mobile number whose length is same as the length of array. Loop through the array array and add the mobile key and value from mobNum array
var mobNum=[1,2];
var array = [{
"name": "Siddhesh mishra",
"add": "hjhjjdjkhjibf"
}, {
"name": "Brijesh mishra",
"add": "jkfhgfbrfhiurf"
}]
array.forEach(function(item,index){
item.mobile=mobNum[index]
})
console.log(array)
var data = [ {"name":"Siddhesh mishra","add":"hjhjjdjkhjibf"},
{"name":"Brijesh mishra","add":"jkfhgfbrfhiurf"} ];
data.map(function(entry){
//add logic to get mobile number from entry.name or entry.add
return entry.mobile = '98989898';
})
console.log(data);
I have an object element and an array element which contains some items of the object.
I would like to delete the items in the object referenced by the array.
var array = ["test1","test2"];
var object =
...
"test1": {
"na": [
"t",
"t-t",
"t-98",
"t"
]
},
"test2": {
"python": [
"jjj"
]
}
...
When I use
delete object.test1
It works.
However in my case, I want :
for(var i = 0 ; i < array.length ; i++){
delete object.array[i];
}
But I got :
object.array is undefined
Any ideas ?
Fiddle
Use object[array[i]], object.array does not exist
If you are using lodash or underscore you can also use the _.omit function.
object = _.omit( object, array )
You would need to use array object notation.
delete object[array[i]]
Array notation is the only way to retrieve property values if you are indexing using a string value.
I have an array like the one you can see in the pic. I have seen array with objects in it, array with numbers and/or strings. But I don't know how to deal with this kind of array. It looks like each line is an object in the array but there is no "{}". (Sorry, I am new to javascript)
My question is..I want a new array like below, how can I convert it?
newArray = [
{
time:Q2.14,
percent:...
},
{
time:Q3.14,
percent:...
},
{
time:Q4.14,
percent:...
},
....
]
The percent is the value for the current time devided by sum of all numbers.
Appreciate!
That's not really an array. It's an object with sub-objects.
var outputArray = [];
for( var prop in notQuiteArray ) {
outputArray.push({time: prop, percent: notQuiteArray[prop]});
}
where notQuiteArray is the object you were inspecting.
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';