0

So I have an object. And what I want to do is search within the object for an ID and return it's fellow elements. What is my best course of action for something like this?

Looping through the object and checking each ID?

var questions = [
{
    id          : 'citizenship-1',
    category    : 'citizenship',
    label       : 'Are you an American Citizen?',
    options     : ['Yes','No'],
    forceAnswer : false
   },
{
    id          : 'citizenship-2',
    category    : 'citizenship',
    label       : 'Do you live in the United States?',
    options     : ['Yes','No'],
    forceAnswer : false
}
];

So what I want to do is tell my javascript function to search for citizenship-2 in var questions and return the id, category, label, etc.

{
id          : 'citizenship-2',
category    : 'citizenship',
label       : 'Do you live in the United States?',
options     : ['Yes','No'],
forceAnswer : false
}

I'm primarily using javascript but also have jQuery available, if there is a better solution within.

Mike
  • 419
  • 1
  • 5
  • 14

3 Answers3

1

You can use .filter() function fer getting the object with a specified value

var filtered = $(questions).filter(function (i, n) {
    return n.id === 'citizenship-2';
});
console.log(filtered[0].category);

Fiddle

Anoop Joshi P
  • 25,373
  • 8
  • 32
  • 53
0

Just a little bit of fun with filter:

var questions = [
{
    id          : 'citizenship-1',
    category    : 'citizenship',
    label       : 'Are you an American Citizen?',
    options     : ['Yes','No'],
    forceAnswer : false
   },
{
    id          : 'citizenship-2',
    category    : 'citizenship',
    label       : 'Do you live in the United States?',
    options     : ['Yes','No'],
    forceAnswer : false
}
];

var id = 'citizenship-1';
var res = questions.filter(function(e) {
    return e.id == id && e;
}, []);

console.log(res);

In this way, if there are duplicates, they will be in the array.

Fiddle: http://jsfiddle.net/mj65n06y/

(results are in the console)

Output:

http://prntscr.com/7h8152 enter image description here

briosheje
  • 7,356
  • 2
  • 32
  • 54
  • @mplungjan: the idea is to return an array of objects, so that if there are duplicates you can detect them, that's why I've added the ,[] , what's wrong? – briosheje Jun 15 '15 at 14:25
  • I understand what you did, but the user asks to get the elements. I would like to see what the `&& e` accomplishes. My version http://jsfiddle.net/mplungjan/b735wcbb/ – mplungjan Jun 15 '15 at 14:31
  • @mplungjan: well, your solution surely looks better. the && e asserts that e.id is equal to id. If so, it returns the entire object (it push it inside the new array []), else it literally does nothing at all, that's just what it does. While my solution just returns an array containing the object(s), your solution provide the object itself with the properties ready-to-use. – briosheje Jun 15 '15 at 14:48
0

Try jQuery's $.param(object) function:

var questions = [
{
    id          : 'citizenship-1',
    category    : 'citizenship',
    label       : 'Are you an American Citizen?',
    options     : ['Yes','No'],
    forceAnswer : false
   },
{
    id          : 'citizenship-2',
    category    : 'citizenship',
    label       : 'Do you live in the United States?',
    options     : ['Yes','No'],
    forceAnswer : false
}
];

window.search = function() {
  var object;
  for (q in questions)
    {
    if (questions[q].id == $('#query').val())
      object = questions[q];
    }
  
    alert($.param(object));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="query" placeholder="Type Object ID" />
<button onclick="search()">Search</button>
omikes
  • 8,064
  • 8
  • 37
  • 50