0

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
2

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.