0

Is there a way to check if my object contain array? for example:

let object1 = {
    name:'abc',
    items:[
        {item_name:'123'},
        {item_name:'456'}
    ]
}

Imagine object1 is coming from server and I am not sure that it will or will not (or even have more) array in it. Is there a proper way to do this?

3
  • 2
    Do you mean check if it has that specific items array in it? Or just any array? Commented Feb 11, 2019 at 5:40
  • How deep does it need to check? For example, does this object contain an array ~ { foo: { bar: [1,2,3] } }? Commented Feb 11, 2019 at 5:40
  • I fail to see how this question has garnered two upvotes. It contains ambiguous requirements and zero research effort. Commented Feb 11, 2019 at 5:53

5 Answers 5

3

You can use Array.prototype.some and Object.values() to iterate and determine if any value is an array.

let object1 = {
    name:'abc',
    items:[{item_name:'123'},{item_name:'456'}]
}

let res = Object.values(object1).some((val) => Array.isArray(val));
console.log(res);

Sign up to request clarification or add additional context in comments.

Comments

2

You can use Object.keys()

The Object.keys() method returns an array of a given object's own property names, in the same order as we get with a normal loop.

and Array.prototype.some()

The some() method tests whether at least one element in the array passes the test implemented by the provided function.

with Array.isArray()

The Array.isArray() method determines whether the passed value is an Array.

let object1 = {
  name:'abc',
  items:[
    {item_name:'123'},
    {item_name:'456'}
  ]
}

var r = Object.keys(object1).some(i => Array.isArray(object1[i]));
console.log(r);

Comments

1

You can use Some and Array.isArray

let object1 = {name:'abc',items:[{item_name:'123'},{item_name:'456'}]}

let op = Object.values(object1).some(e=>Array.isArray(e))

console.log(op)

2 Comments

At least when your answer was using Object.keys, it was different to the other answers
@Phil i realized that in this case using values is a better. because we need to access values any how
1

Use isArray(). It tells if an object is an array or not. Refer

let object1 = {
name:'abc',
items:[
{item_name:'123'},
{item_name:'456'}
]
}
Object.values(object1).forEach((e)=>{
if(Array.isArray(e))
{
console.log('true')
}})

1 Comment

FYI: Returning from the forEach callback does nothing. This will once again perform unnecessary comparisons after finding the first array
0

Use find and isArray on Object.values, then convert length to Boolean:

let object1 = {
  name:'abc',
  items:[
    {item_name:'123'},
    {item_name:'456'}
  ]
}

console.log(Boolean(Object.values(object1).find(e => Array.isArray(e)).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.