1

I'd like to get 'roomname' out of the arrangement.. But when I run the log, I get undefined.

var roomNameArr = presenterArray.filter(function(item){
   console.log("## item.roomName : "+item.roomName);
   return item.roomName === viewerRoomName;
});

When I ran the log, the results below came out.

console.log("## roomNameArr : "+JSON.stringify(roomNameArr));

roomNameArr = [{"id":"1","pipeline":{"domain":null,"_events":{},"_eventsCount":7,"id":"5465b70f-613f-4184-80d3-895b3dcfa2c7_kurento.MediaPipeline"},"webRtcEndpoint":{"domain":null,"_events":{},"_eventsCount":6,"id":"5465b70f-613f-4184-80d3-895b3dcfa2c7_kurento.MediaPipeline/ac2bc699-bddf-44e2-a711-6444737109a1_kurento.WebRtcEndpoint"},"presenterRoomNum":"1","roomName":"1"}]

When I take a log to take out variable a, 'undefined' comes out.

console.log("## roomNameArr1 : "+roomNameArr['roomName']);
console.log("## roomNameArr1 : "+roomNameArr['5']);

I'm still wandering around and I need you to help me.

1
  • 2
    I'm assuming you are getting undefined on the last console.log. There is no roomName property in the oomNameArr array. Only on the objects inside it. roomNameArr[0]['roomName'] should show 1. Also, if you want a single result, you can use Array.prototype.find instead of filter Commented Mar 26, 2019 at 5:19

1 Answer 1

1

As roomNameArr is an array of objects, there is always an index number to all the objects within that array.

So if you want to access the key:value of any object within the roomNameArr array, you must have to define the index number on that object exists.

In your case, you have to write something like

console.log("## roomNameArr1 : "+roomNameArr[0]['roomName']);

or

console.log("## roomNameArr1 : "+roomNameArr[0].roomName);

to get the output correctly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.