0

I've look at a few other posts but am still slightly confused about using arrays within arrays.

So I've created my array as below:

var pagesArray = [
    {
        category: 'pages1',
        pages: [
            'firstPage1',
            'secondPage1',
            'thirdPage1',
            'fourthPage1'
        ]
    }, {
        category: 'pages2',
        pages: [
            'firstPage2',
            'secondPage2',
            'thirdPage2',
            'fourthPage2'
        ]
    }
];

Now I want to be able to search the array and find if a string exists in it. If it does, I want it to return the value of what position it is.

E.g.

jQuery.inArray('secondPage1', pagesArray)

to give the result:

pagesArray[0][1];

I'm not sure if I've written the array wrong and if I'm using inArray correctly. If I try to alert(pagesArray[0][1]); it gives a value of undefined. What am I doing wrong here?

3
  • you can check the validity of your array by doing console.log(pagesArray), then you know if the array is indeed correct. Commented Aug 12, 2013 at 11:16
  • pagesArray[0][pages] is what I think you'd need. Commented Aug 12, 2013 at 11:16
  • Please look here - stackoverflow.com/questions/1758937/… Commented Aug 12, 2013 at 11:19

3 Answers 3

1

Updated Demo

$.each(pagesArray, function (i, v) {
    if((pos=$.inArray('secondPage1', pagesArray[i]['pages'])) !== -1){
        console.log('pagesArray['+i+"]['pages']"+'['+pos+']');
    };
});

output pagesArray[0]['pages'][1]

pagesArray[0] means found in first array [0] 0 means first

pagesArray[0]['pages'][1] found in first array [0] inside sub array ['pages'] at index [1] Second position as index starts from 0

Demo

$.each(pagesArray, function (i, v) {
    if($.inArray('secondPage1', pagesArray[i]['pages']) !== -1){
        alert('inarray');
    };
});

to access category

console.log(pagesArray[1]['category']);

to access pages

console.log(pagesArray[1]['pages']);

to find values in array you can use

console.log($.inArray('secondPage1', pagesArray[0]['pages']));
Sign up to request clarification or add additional context in comments.

2 Comments

This is great thanks. But Is there a way for it to tell me where it is in the array. For example if I search for secondPage1, I need it to tell me it's in the category pages1 and is the first item in that part of the array.
Aaah I've worked out how to do that last bit now. I can just return the 'i' value. Thanks for your help, this was a useful answer.
0

Try this:

var pagesArray = [
    {
      category: 'pages1',
      pages: [
        'firstPage1',
        'secondPage1',
        'thirdPage1',
        'fourthPage1' ]
    }, {
      category: 'pages2',
      pages: [
        'firstPage2',
        'secondPage2',
        'thirdPage2',
        'fourthPage2' ]
    }
  ];

for(var i = 0; i < pagesArray.length; i++)
{
    var exist = jQuery.inArray("firstPage2", pagesArray[i]);
    if(exist)
    {
        alert('Got the Value!!!!');
        break;
    }
}

Fiddle

Comments

0
function findInArray( arr, value ) {
   var j; 
   for ( var i in arr ) {
      if ( ~( j = arr[ i ].pages.indexOf( value ) ) )
         return { i: +i, j: +j };
   }
}

findInArray( pagesArray, 'thirdPage2' );
>> Object {i: 1, j: 2}

So it is pagesArray[ 1 ].pages[ 2 ]. Then:

var p = findInArray( pagesArray, 'thirdPage2' );
pagesArray[ p.i ].pages[ p.j ];
>> 'thirdPage2'

Accordingly you can write a method that returns the value or nothing to check whether an item exists:

function findInArray( arr, value ) {
   var j; 
   for ( var i in arr ) {
      if ( ~( j = arr[ i ].pages.indexOf( value ) ) )
         return arr[ i ].pages[ j ];
   }

   return false;
}

findInArray( pagesArray, 'foo ' );
>> false
findInArray( pagesArray, 'fourthPage2' );
>> 'fourthPage2'

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.