0

I have json array is as below

[
    {
        French: 'Hello',
        Spanish: 'Hello1',
        english:'Hello2'
    },{
        French: 'Hello3',
        Spanish: 'Hello4',
        english:'Hello5'
    },{
        French: 'Hello6',
        Spanish: 'Hello7',
        english:'Hello8'
    },{
        French: 'Hello9',
        Spanish: 'Hello10',
        english:'Hello81'
    }
];

In Javascript array If I wanted to search items based on values

ex If I give Hello6 as finding string I should get 3rd item in the list. I would like generic search rather searching each and every element.

1
  • "...generic search rather searching each and every element" What does that mean? What have you tried? Where are you stuck? Commented Jan 31, 2014 at 15:46

4 Answers 4

2

You can try this:

function findIndex(arr, str) {
    for (var i = 0; i < arr.length; i++) {
        for (var key in arr[i]) {
            if (arr[i][key] === str) {
                if (arr[i].hasOwnProperty(key) {
                    return arr[i];
                }
            }
        }
    }
    return null;
}

This method consists of an array search with a normal for loop and then, for each element of this array, we perform a for..in loop.

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

3 Comments

It's probably unnecessary to check for ownProperty in json object. However it's a good habit.
Indeed, it is. I just put it there because I wanted to write a generic function that can search through any object, either JSONs or not.
It's not really generic when you use .hasOwnProperty() like that. It's now specific to the only the case where you're certain you don't want inherited properties. To make it generic, you'd need a second parameter used to indicate if it should be restricted to own properties.
1

Here is my sample using native functions:

var items = [/*your sample*/];
var myValue = "Hello6";

var result = items.filter(function (item) { 
    return Object.keys(item).some(function (property) { 
        return item[property] === myValue; 
    });
});

Comments

0

you can use filter function:

var val = "Hello6";
//if you want to filter the array
resultArray = jsonArray.filter(function(obj, index){
    for(var key in obj){
        if(obj[key]==val) return obj;
    }
});

var indexes=[];
//if you want to find the results
jsonArray.forEach(function(obj, index){
    for(var key in obj){
        if(obj[key]==val){
            indexes.push(index);
            break;
        }
    }
});

Comments

0

You could use good old for loops :

function find(array, fn) {
    for (var i = 0, l = array.length; i < l; i++) {
        if (fn(array[i]) === true) return array[i];
    }
}

Usage example :

var a = [{ a: 1 }, { a: 2 }, { a: 3 }];
var result = find(a, function (item) {
    return item.a === 2;
});
result; // { a: 2 }

With a more complex array :

var a = [
    { a: 1, b: 2 }, 
    { a: 3, b: 4 }, 
    { a: 5, b: 6 }
];
var result = find(a, function (item) {
    for (var k in item) {
        if (item[k] === 4) return true;
    }
});
result; // { a: 3, b: 4 }

2 Comments

=== true. Do you think it is necessary?
No it's not. I just think it's a bit easier for the OP to understand.

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.