0

I created a class in JavaScript like

function chat(data){
this.id = data.id;
this.name = data.name;
this.message = data.message
this.date = data.date;
}

Now i declare an array like this.

     var message = [];

and push my chat object in message array;

var chat = new chat(data);
message.push(chat);

What is the best possible way to find the elements in message array by id. Or any thing else which make it easier also i need to short the array using date also.

2
  • possible duplicate of JSON - Javascript - How to search array item by searching list of values Commented Feb 1, 2014 at 7:18
  • for what purpose do you want to use this function.. If you are using it for an application … you can use AngularJS . as it gives filter and sorting functions.. otherwise you can create a function to loop over and check... Commented Feb 1, 2014 at 8:24

1 Answer 1

0

Loop. Since chat is an object you cannot use Array.indexOf.

If you instead do

var Message = {};
Message[data.id]=new chat(data);

If chat() is as simple as you show, then just do

Message[data.id]=data;

Then you can access either directly by id

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.