1

I currently have an object that is returning this:

Object {1: 0, 2: Array[2], 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0}

What I am trying to do is 'replace' the "Array[2]" with just the number "2" (counting how many records are in the array)

3 Answers 3

3

Here is the code

var obj = {1: 0, 2: [0, 1], 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0};

Object.keys(obj).forEach(function(key) {
    if (Array.isArray(obj[key])) {
        obj[key] = obj[key].length;
    }
});
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

Object.keys(your_object).forEach(function(index){
    if (toString.call(your_object[index]) === '[object Array]') {
        your_object[index] = your_object[index].length;
    }
});

Comments

0

You can do this to convert all arrays to it's equivalent length.

for( var key in obj ) {

  if( obj[key] instanceof Array ) {
    obj[key] = obj[key].length;
  }

}

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.