I'd like to add the Object to the array of object.
My array is in jsfiddle: https://jsfiddle.net/5w4zhw92/
The Expected
var sensors = [
{ id: 'led', name: 'LED', type: { port: '', path: '' } },
{ id: 'temp', name: 'TEMP', type: { path: '' } },
];
asked May 14, 2015 at 4:49
Kanin Peanviriyakulkit
5201 gold badge6 silver badges29 bronze badges
2 Answers 2
It's already working you just need to declare the variable before using it.Declare sensorType first then use it.
var sensorType = {
sender: {
port: '',
path: ''
},
receiver: {
path: ''
}
};
var sensors = [
{ id: 'led', name: 'LED', type: sensorType.sender },
{ id: 'temp', name: 'TEMP', type: sensorType.receiver }
];
console.log(sensorType.sender);
console.log(sensors);
answered May 14, 2015 at 4:53
dReAmEr
7,2147 gold badges40 silver badges67 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Just change order of statements in your code. Declare sensorType object first.
var sensorType = {
sender: {
port: '',
path: ''
},
receiver: {
path: ''
}
};
var sensors = [
{ id: 'led', name: 'LED', type: sensorType.sender },
{ id: 'temp', name: 'TEMP', type: sensorType.receiver },
];
console.log(sensorType.sender); //Returns Object {port: "", path: ""}
console.log(sensors); //[{ id: 'led', name: 'LED', type: { port: '', path: '' } },{ id: 'temp', name: 'TEMP', type: { path: '' } }];
answered May 14, 2015 at 4:54
Gilsha
14.6k3 gold badges36 silver badges48 bronze badges
Comments
lang-js
sensorTypebefore it is used