2

I have JavaScript array contains links something like this:

var urls = ['url1','url2','url3'];
  if (urls.indexOf(window.location.hostname) > -1)
{
  // do something ...
}
else {
  window.location.href = 'redirect url';
}

this code work well, but I tried to convert it to an array of objects like this:

var urls = [
  {
    'url':'url1'
  },
  {
    'url':'url2'
  },
  {
    'url':'url3'
  },
];
if (urls.url.indexOf(window.location.hostname) > -1)
{
  // do something ...
}
else {
  window.location.href = 'redirect url';
}

But this code is not working!!

How I can convert the first code into array of objects, or how I can search in array?

2
  • 1
    that's not JSON for a start, it's just a regular javascript object ... but then your search needs to be smarter Commented Feb 20, 2016 at 8:23
  • You could useca for loop to iterate through the items, and inspect each item if it is a match. Commented Feb 20, 2016 at 8:23

5 Answers 5

1

Easiest solution is to use array.some

if(urls.some(function(item) { return item.url == window.location.hostname;})) {
    // do something ...
} else {
    window.location.href = 'redirect url';
}

more readably

var found = urls.some(function(item) { 
    return item.url == window.location.hostname;
});
if(found) {
    // do something ...
} else {
    window.location.href = 'redirect url';
}
Sign up to request clarification or add additional context in comments.

Comments

0

First of all: Both definitions of your urls variable are JSON:

  1. first definition is an array of Strings in JSON notation
  2. second definition is an array of objects, each consisting of one property url as a string.

In order to search through the second one, you need to iterate over the objects array explicitly and compare the property explicitly (as one approach):

var found = false;
for ( var n = 0; n < urls.length; n++ )
{
    if ( urls[n].url == window.location.hostname )
    {
       /// do something

       found = true;
       break;
    }
}
if ( !found )
{

  // do something other

}

2 Comments

JSON is textual data interchange format, there's no JSON in the question.
There has been as I typed my answer, see edits of OP
0

Or may be

if(urls.map(function(obj){ return obj.url}).indexOf(window.location.hostname)>0){
  // do something ...
}
else {
  window.location.href = 'redirect url';
}

Comments

0

Using lodash version 4 or later, you could do this:

if (_(urls).map('url').includes(window.location.hostname)) {
  // do something...
} else {
  window.location.href = 'redirect url';
}

Comments

0

When it comes to arrays of objects you cannot access elements like what you have done in your code. You have to use a loop to travers through the elements or an inbuilt functions like filter,map.Without using inbuilt functions you can do something like this to get your work done.

function to search for a particular url in the array

function findURL(array,url) {

    for (var i = 0; i < array.length; i++) {

        if (array[i].url == url) {
            return i;
        }
    }
    return -1;
}

Put below piece of code where you want to check the condition

var urls = [
    {
        'url':'url1'
    },
    {
        'url':'url2'
    },
];

if(findURL(urls,window.location.hostname) > -1){
    //do something ..
}else{
    window.location.href = 'redirect url';
}

There are some answers which describe how to apply filter and map methods in your scenario.Therefore I think i don't want to put those in my answer.

If you use jquery there is a function called grep which returns an array of items found.

var urls = [
    {
        'url':'url1'
    },
    {
        'url':'url2'
    },
];

var result = $.grep(urls, function(e){ return e.url == window.location.hostname; });

if (result.length == 0) { // nothing found

   window.location.href = 'redirect url';

} else if (result.length == 1) { // one item found

      //do something ..
      // you can access the found item by result[0].foo if you want

} else { // multiple items found
  
}

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.