1

How can I differentiate if an array is a simple array?

f.e

let simpleArr = ["hi","1",3,"5","this is a string",29999]

or an array of objects

f.e

let objectArr = [
 {el1: "hi", el2: "hi"},
 {el1: "hi2", el2: "hi2"},
 {el1: "hi3", el2: "hi3"},
 {el1: "hi4", el2: "hi4"},
]

Is the below enough?

const isObject = (obj) => typeof obj === 'object' && obj !== null

isArray(objectArr) && objectArr.every(isObject)
5
  • 2
    don't forget "function": github.com/lodash/lodash/blob/… Commented May 14, 2021 at 10:23
  • 2
    If you want to check if all members are objects - yes. But this will return false when the array contains objects and other data, e.g. [{}, true, {}]. Is that expected? Commented May 14, 2021 at 10:23
  • const isSimpleArray = arr => !arr.some(x => typeof x === 'object') Commented May 14, 2021 at 10:24
  • 1
    @georg yeah, that as well. If you want any objects, then you need to include functions as they are also objects. Commented May 14, 2021 at 10:24
  • @boxdox [null] -> false Commented May 14, 2021 at 10:25

3 Answers 3

1

You can do this.

const isObject = item => typeof item === "object" 
                          && !Array.isArray(item) 
                          && item !== null;

const simpleArr = ["hi","1",3,"5","this is a string",29999];
const array_of_objects = [{el1:"hi",el2:"hi"},{el1:"hi2",el2:"hi2"},{el1:"hi3",el2:"hi3"},{el1:"hi4",el2:"hi4"}];

console.log(simpleArr.every(isObject)); // false
console.log(array_of_objects.every(isObject)); // true

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

Comments

0

Maybe it's not a straight forward approach, but you can try doing a deep copy of the Array. You see in javascript comparison between non primitive values like objects will return false even if they both have the exact same properties.

{foo: "bar"} === {foo: "bar"} // false

const simpleArr = ["hi","1",3,"5","this is a string",29999]
const objectArr = [
 {el1: "hi", el2: "hi"},
 {el1: "hi2", el2: "hi2"},
 {el1: "hi3", el2: "hi3"},
 {el1: "hi4", el2: "hi4"},
]

function deepCopy(array) {
  return JSON.parse(JSON.stringify(array));
}

function isSimple(array) {
  const copy = deepCopy(array);
  return array.every((el, index) => el === copy[index]); 
}

console.log("simpleArr:", isSimple(simpleArr))
console.log("objectArr:", isSimple(objectArr))

1 Comment

Don't do that. Too much work and it's prone to failure as you can lose data which can skew your results. jsbin.com/zatucuk/edit?js,console
0

Yes, you can do it like so. Just make sure to do a check for Function as well.

let simpleArr = [
  "hi",
  "1",
  3,
  "5",
  "this is a string",
  29999,
  Function,
];

let objectArr = [{
    el1: "hi",
    el2: "hi",
  },
  {
    el1: "hi2",
    el2: "hi2",
  },
  {
    el1: "hi3",
    el2: "hi3",
  },
  {
    el1: "hi4",
    el2: "hi4",
  },
];

const isSimpleArray = arr =>
  !arr.some(
    x => x != null && (typeof x === "object" || typeof x === "function")
  );

console.log(isSimpleArray(simpleArr));
console.log(isSimpleArray(objectArr));

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.