JSON - Javascript - How to search array item by searching list of values -


i have json array 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 wanted search items based on values

ex if give hello6 finding string should 3rd item in list. generic search rather searching each , every element.

you can try this:

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

this method consists of array search normal for loop , then, each element of array, perform for..in loop.


Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -