0

I have the following object and what I would like achieve is to get the index of theme if the name has match with a variable.

for example: I'm making a loop in the views and if my task (something1) variable has matches with the name element than to return the index of object.

By the given example I should have as result 0,

var views = [

    {
        name: "something1",
        type: something1,
        columns: something1
    },

    {
        name: "something2",
        type: something2,
        columns: something2
    },

    {
        name: "something3",
        type: something3,
        columns: something3
    }

];


var task = 'something1';

$.each(views, function(index, value) {

    if (value.name = task) {
        alert(index);
    }

}); 
3
  • That should work, provided you use commas to separate your objects and your references resolve, e.g. something1 exists. Commented Oct 13, 2011 at 11:49
  • @alex, Ide like to hope that those variables are for examples sake only, and not real names Commented Oct 13, 2011 at 12:00
  • Yes but the actually the alert ouptuts all the indexes in the object. Commented Oct 13, 2011 at 12:07

3 Answers 3

1

You dont really need jQuery for this:

See: http://jsfiddle.net/enNya/2/

var views = [
    {
        name: "something1",
        type: "something1",
        columns: "something1"
    },
    {
        name: "something2",
        type: "something2",
        columns: "something2"
    }
];

var task = 'something2';

// Set a var and maintain scope
var i;

// Loop each element of the array
for (i = 0; i < views.length; i++) {
    // If the X = Y the stop looping
    if (views[i].name == task) {
        break;
    }
}

// Check if it was not found
i = i == views.length ? false : i;

// Log the result
console.log(i);
Sign up to request clarification or add additional context in comments.

Comments

1

It's just a matter of syntax, as lgt said don't forget toseparate elements within your object with commas. Aslo the correct 'equal' operator is '=='. 'value.name=task' would be always true. It means can I affect the content of 'task' into 'value.name'.

Here is your valid js.

Note that in this example you'll get 2 alertbox.. ;)

var views=[

{
name:"something1",
type:'something1',
columns:'something1'
},

{
name:"something1",
type:'something1',
columns:'something1'
},

{
name:"something2",
type:'something2',
columns:'something2',
},

];


var task='something1';

$.each(views, function(index, value) { 

        if (value.name==task){

            alert(index);
        }
}); 

Comments

0

replace something1 variable for 0 and value.name == task (double =)

var views=[{
    name:"something1",
    type:0,
    columns:0
}, {
    name:"something1",
    type:0,
    columns:0
}, {
    name:"something2",
    type:0,
    columns:0
}];

var task='something1';
$.each(views, function(index, value) { 
    if (value.name==task){
        return index;
    }
}); 

2 Comments

indeed ... should be return index;
errr, correct me if Im wrong but isnt return totaly pointless in $.each (except in the sence of a break)

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.