James
James

Reputation: 37

Check if javascript object key value is an array or string?

I have an array as follows.

Array
   (
    [13] => Array
    (
        "eventName" => Array
        (
            [0] => 'Event Name1'
            [1] => 'Event Name2'
        )
        "eventInfo" => Array
        (
            [0] => 'Event Information1'
            [1] => 'Event Information2'
        )       
    )
    [20] => Array
    (
        "eventName" => 'Event Name1'
        "eventInfo" => 'Event Information1'
    )
)

I will be using this data to push information into a calendar; the first level of the array represents the date. I send the content as a json string to javascript and then parse. Once the content is an object, how do I determine if the value of key "eventName" is a string or array. So for I have

var object = JSON.parse(data);
var objectSize = Object.keys(object).length;

for(var i = 0; i < objectSize; i++){
    var id = Object.keys(object)[i];
    if(Object.keys(object[id].eventName) != string){ //How do I fix this line?
        var arraySize = Object.keys(object[id].eventName).length;
        for(var r = 0; r < arraySize; r++){
            //update calendar cell
        }
    } else {
        //update calendar cell
    }
}

How do I fix this line?

if(Object.keys(object[id].eventName) != string){

I tried this with no luck.

if(Array.isArray(Object.keys(object[id].eventName))){

Upvotes: 1

Views: 1108

Answers (2)

matvs
matvs

Reputation: 1873

Array.isArray(Object.keys(object[id].eventName)) will always return true, since:
The Object.keys() method returns an array of a given object's own enumerable property names

You need to check object[id].eventName directly:

Array.isArray(object[id].eventName)

I would also like to suggest changing the data structure, since part of the code seems to be repeated depending on whether eventName is a string or an array, you could just always use the latter, because there is nothing wrong with iterating over an array, that contains only one element.

Another improvement could be iterating over Object.keys(object) array directly. Could be done with:

  1. forEach function of an array object
  2. regular for loop
  3. for..of

let object = { foo: 'bar', x: 10 }

// forEach
Object.keys(object).forEach(key => console.log(key));

const keys = Object.keys(object); 
// for
for (let i = 0; i < keys.length; ++i) {
  console.log(keys[i])
}

// for...of 
for (let key of keys) {
  console.log(key);
}

Since you actually iterate over object/map/dictionary the standard way to do it in JS is for...in loop.
It may require hasOwnProperty() check if you are dealing with more complex types.

let object = { foo: 'bar', x: 10 }

for (let key in object) {
  console.log(key);
}

Upvotes: 2

Dimitri Kopriwa
Dimitri Kopriwa

Reputation: 14375

You can do :

const dataJs = [
  {
    "eventName": ['Event Name1', 'Event Name2'],
    "eventInfo": ["Event Information1", "Event Information2"],
  },
  {
    "eventName": "Event Name1",
    "eventInfo": "Event Information1",
  }
];


for(var i = 0; i < dataJs.length; i+=1){
    if(typeof dataJs[i].eventName !== 'string'){ //How do I fix this line?
        var arraySize = dataJs[i].eventName.length;
        for(var r = 0; r < arraySize; r++){
            //update calendar cell
        }
    } else {
        //update calendar cell
    }
    var contentSize = dataJs[i].eventName.length;
}

Upvotes: 0

Related Questions