2

So basically i want to check if my data (which is in JSON Format) has a value which is a primitive. So let's take an Example: I get data that looks like this: {name: Artikel, value: {"ArtNr": 1234}} and i want to check if 1234 is primitive or not. I also want to differentiate if the result is an Array with Primitives in it or an Object. Is that possible?

function isObjectContainingPrimitiveValues(test) {
        let values = Object.values(test);
        for (let i of values) {
            console.log(i);
            return (typeof i === 'string' || typeof i === 'number' || typeof i === 'boolean' || typeof i === null || typeof i === undefined);
        }
    }

UPDATE

So with the awesome help of MaxK i have built a isResultContainingPrimitiveValues() Function which checks my data for Primitive/ Primitive Arrays and or Objects. The following part is the trickiest at least with my understanding. The following Example will hopefully help you understand my problems better.

So my let namesSplit = treeNode.name.split('.'); variable splits the data it gets and has as a result of nameSplit : Artikel,Artnr. Next i defined a key variable let key = namesSplit[0]; which has key : Artikel as a result. Than i define a contextEntry variable let contextEntry = exprData.contextEntry.find(_x => _x.name === key); and has contextEntry : {"name":"Artikel","value":{"ArtNr":123}} as a result. Now i want to check: if there's another split namesSplit.length > 1 check isResultContainingPrimitiveValues(). If it is primitive, throw an error, if it is an object -> get values from it and if it is an array -> get values form there. I know it's a lot but from all the confusing stuff i can't seem to think clear, so i appreciate every help i can get.

OTRAY
  • 115
  • 2
  • 11
  • I'm not exactly sure what the question is. But Object accepts a parameter and returns an Object so if Object(test) !==test then test is primitive – Max K Feb 20 '20 at 14:08
  • So i get back an Object which has a key and a value in it (example: 'ArtNr': 1423). And i want to check the value against my isPrimitive funciton – OTRAY Feb 20 '20 at 14:12
  • 2
    `typeof i === null || typeof i === undefined` won't happen (the `typeof` operator **always** returns a string). I think you want `typeof i === 'undefined' || i === null`. – Dai Feb 20 '20 at 14:16
  • 1
    Your function is named `isPrimitive` but it accepts an `object` argument - you should rename the function to `isObjectContainingOnlyPrimitiveValues` or something more specific, because an `object` value (besides `null`) is not a primitive value. – Dai Feb 20 '20 at 14:18
  • Thanks and i'll try that. (I also have updated my question, i hope it's more clear now) – OTRAY Feb 20 '20 at 14:19
  • You are right i just did that, thanks – OTRAY Feb 20 '20 at 14:21
  • Does this answer your question? [Check if a value is an object in JavaScript](https://stackoverflow.com/questions/8511281/check-if-a-value-is-an-object-in-javascript) – Tom O. Feb 20 '20 at 14:38
  • I've seen that post (before i created mine) but apparently it does not help me... – OTRAY Feb 20 '20 at 14:45

2 Answers2

2

You are returning from your function on the first iteration. You should only return false if you found an non-primitive and if you were able to loop over all values you can return true because all values are primitives:

function isObjectContainingPrimitiveValues(testObj) {
  let values = Object.values(testObj);
  for(let i of values){
     if (typeof i === 'object') {
       return false;
     }
  }
  return true;
};

Update:

After reading your comment i changed the code to check for arrays with primitives as well. The idea is, to create a new function which only checks if a single value is a primitive.Now if we find an array, we can simply check - with the help of the arrays some function - if some element, inside the array is not primitive. If so return false,otherwise we do the same checks as before:

function isObjectContainingPrimitiveValues(testObj) {
  let values = Object.values(testObj);
  for (let i of values) {
    if (Array.isArray(i)) {
      if (i.some(val => !isPrimitive(val)))
        return false;
    } else {
      if (!isPrimitive(i))
        return false;
    }
  }
  return true;
};

function isPrimitive(test) {
  return typeof test !== 'object'
}
Max K
  • 1,062
  • 9
  • 11
  • Ok i'll try your solution, but i also need to differentiate if my Data object is an array with primitives in it , how would i go about this? – OTRAY Feb 20 '20 at 14:40
  • So you want isObjectContainingPrimitiveValues to also return true if some values are arrays with only primitives? – Max K Feb 20 '20 at 14:47
  • Yes exactly but i don't have any idea – OTRAY Feb 20 '20 at 15:00
  • Thank you very much i also have one last question if you got the time (sorry for always updating my question) – OTRAY Feb 20 '20 at 15:47
  • Sorry, i don't get it. So you checked contextEntry with the isObjectContainingPrimitiveValues and now you want to do the same procedure for every element in namesSplit ? – Max K Feb 20 '20 at 16:24
  • I know it‘s a little bit complex and probably didn’t explain it well too but we build an expression language and we get back the data in a tree structure which we recursive check. The Tree Structure is the namesSplit and the data is the contextEntry – OTRAY Feb 20 '20 at 17:21
0

Array and object types all return a 'typeof' 'object'. so you can check against an object instead of checking against multiple conditions. So the return statement will be: return (typeof i === 'object'). Number, string, undefined, null will all return false on the statement above.

Eliasu
  • 79
  • 2
  • Why? I don't get it 100%, would you mind explaining it a little more detailed? Why would Number, String, etc return false? – OTRAY Feb 20 '20 at 14:26
  • The value that get's back could also have an result which looks like this: { name: "Artikel", value: {[Artnr: 1234]}} – OTRAY Feb 20 '20 at 14:33
  • I downvoted this answer because it's incorrect. Run this in your console: `var x = null; console.log( typeof x );` will output `'object'`. See here: https://2ality.com/2013/10/typeof-null.html – Dai Feb 21 '20 at 01:37