nourhein chaieb
nourhein chaieb

Reputation: 39

how do i get values from multidimensionnal table in javascript?

i have an array of urls. I insert into it values like this :

 var replacementArray=[];
    function insert(arr,url, shorturl) {
            arr.push({
                url: url,
                shorturl: shorturl
            });
        }

This is an example of one such URL array:

 replacementArray:{
    [url:"tuto.com", shorturl:"xfm1")],
    [url:"youtube.com", shorturl:"xfm2")], 
    [url:"google.com",shorturl:"xfm3"]}

I have to compare the shorturl of this array with a string. If the strings match then i retrieve the url. Here is my first attempt at doing this :

  var chaine="xfm1";//this is an example 

  for(var j=0;j<replacementArray.length;j++)
     if (replacementArray[j][shorturl]==chaine){
         var url= replacementArray[url];
         }

This seems to not be working. Why is that?

Upvotes: 1

Views: 64

Answers (4)

Sᴀᴍ Onᴇᴌᴀ
Sᴀᴍ Onᴇᴌᴀ

Reputation: 8297

Read over these corrections/suggestions:

  1. As others have mentioned, you should create an array of objects, instead of an object with arrays
  2. Reference the property 'shorturl' either using array syntax (i.e. replacementArray[j]['shorturl']) or dot notation (i.e. replacementArray[j].shorturl). If you use array syntax then the property needs to be in a string literal (unless you create a variable to represent the field - e.g. var shorturl = 'shorturl';).

var replacementArray = [];

function insert(arr, url, shorturl) {
  arr.push({
    url: url,
    shorturl: shorturl
  });
}
//utilize the function declared above
insert(replacementArray ,"tuto.com", "xfm1");
insert(replacementArray, "youtube.com", "xfm2");
insert(replacementArray, "google.com", "xfm3");

var chaine = "xfm1"; //this is an example 
var url; //declare url here so it won't be undefined if no url is found in the array
for (var j = 0; j < replacementArray.length; j++) {
  if (replacementArray[j]['shorturl'] == chaine) {
    //need to reference replacementArray[j] instead of replacementArray['url']
    url = replacementArray[j]['url'];
  }
}
console.log('url: ',url);

  1. Consider using Array.prototype.find() or a similar functional-style method (e.g. filter() if you wanted to find multiple values) to determine the site with the matching shorturl value. That way you don't have to worry about creating and incrementing the iterator variable (i.e. j) and using it to reference elements in the array. For more information, try these exercises about functional programming in JS.

var replacementArray = [];

function insert(arr, url, shorturl) {
  arr.push({
    url: url,
    shorturl: shorturl
  });
}
//utilize the function declared above
insert(replacementArray ,"tuto.com", "xfm1");
insert(replacementArray, "youtube.com", "xfm2");
insert(replacementArray, "google.com", "xfm3");

var chaine = "xfm1"; //this is an example 
var foundSite = replacementArray.find(function(site) {
  return (site.shorturl == chaine);
});
if (foundSite) { //if we did find a matching site
    var url = foundSite.url;
    console.log('url: ',url);
}

Upvotes: 0

Jyothi Babu Araja
Jyothi Babu Araja

Reputation: 10292

var arr=[];
function insert(arr,url, shorturl) {
  arr.push({
    url: url,
    shorturl: shorturl
  });
}
insert(arr,"google.com", "xfm.io1"); 
insert(arr,"gogle.com", "xfm.io2"); 
insert(arr,"gole.com", "xfm.io3");

function getUrl(yourVariable){ //chaine
  for(var j=0;j<arr.length;j++)
    if (arr[j].shorturl == chaine){
      return arr[j].url;
    }
  return null;//not found yourVariable
}
var chaine= "xfm.io1"; //your Short URL
console.log(getUrl(chaine)); //testing the function

First of all you given: (which is not an acceptable data structure)

replacementArray:{
    [url:"tuto.com", shorturl:"xfm1")],
    [url:"youtube.com", shorturl:"xfm2")], 
    [url:"google.com",shorturl:"xfm3"]}

which must be like this: (array of objects)

replacementArray:[
    {url:"tuto.com", shorturl:"xfm1"},
    {url:"youtube.com", shorturl:"xfm2"}, 
    {url:"google.com",shorturl:"xfm3"}]

Then to get shortUrl code will be like

function getUrl(yourVariable){ //chaine
    for(var j=0;j<replacementArray.length;j++)
        if (replacementArray[j].shorturl == chaine){
            return replacementArray[j].url;
        }
    return null;//not found yourVariable
}

Upvotes: 1

ppajer
ppajer

Reputation: 3145

Associative arrays with arbitrary keys don't exist in javascript

You can have data that works as an associative array, but then you need to use an object to store the keys.

The example data you provided is not valid JS. It is an object of arrays instead of being an array of objects. For your function to work as expected, the data needs to be in the following format:

[
    {
        url: 'tuto.com',
        shorturl: 'xfm1'
    },
    {
        url: 'youtube.com',
        shorturl: 'xfm2'
    },
    // etc...
]

The [] is for creating an array, which will have numeric indexes only.

The {} creates objects that can store key-value pairs like an associative array in other languages.

So in your function you can loop through the array indexes by incrementing i and access the values associated with your keys using replacementArray[i].shorturl or replacementArray[i]['shorturl'] (notice the string) - the way you do it is up to your preference, they both work the same.

Hope this helps!

Upvotes: 2

user5522551
user5522551

Reputation:

Try this in your 'for' loop:

if(replacementArray[j].shorturl == chaine){
    // Do stuff here...
}

With [shorturl], you are accessing a property name based on the value of shorturl, which is not defined.

Upvotes: 0

Related Questions