Check if the value of an object property is an object - javascript

const opb = {
'e' : 1,
'3' : 2,
'4' : {'ee':12}
}
Object.entries(opb).forEach(el => {console.log(el[1] !== typeof 'object' , typeof el[1])})
Hello im building a check that checks if the first value of an object property is another object.
I am doing this as follow:
const opb = {
'e' : 1,
'3' : 2,
'4' : {'ee':12}
}
Object.entries(opb).forEach(el => {console.log(el[1] !== typeof 'object' , el[1])})
Somehow everything returns true, eventho one should be an object (false).

It should be typeof el[1] !== 'object'.
const opb = {
'e' : 1,
'3' : 2,
'4' : {'ee':12}
}
Object.entries(opb).forEach(el => {console.log(typeof el[1] !== 'object' , el[1])})

Related

Ternary operator return nothing

is there a way to return nothing for ternary operator I mean
const a = [ 0 ? { name : "example" } : null ]
when i print a = [null]
or:
const a = [ 0 && {name:"example"}]
a will be [false]
i expected that a = [] for case 1
You could spread an (empty) array.
console.log([...(0 ? [{ name : "example" }] : [])]);
console.log([...(1 ? [{ name : "example" }] : [])]);
No. It isn't possible. You are going to get a value back.
If you conditionally want a value, then either make the decision outside the array or filter the null values out afterwards.
e.g.
const a = 0 ? [ { name: "example" } ] : [];
or
const a = [ 0 ? { name : "example" } : null ].filter(Boolean);
null is a perfectly valid array value in Javascript, as is undefined.
You can simply construct the array on demand...
const a = 0 ? [{ name : "example" }] : [];
or if this is part of a larger array construction, use splats...
const a = [
'a',
...(0 ? [{name : "example"}] : []),
'b'
];
console.log(a); //-> ['a', 'b']

&& operator returns 0 in JavaScript

I have an array of objects like this.
const items = [{ label : "foo" value : 1 },
{ label : "bar" value : 2 },]
When I have value1 = 1 value2 = 2 value3 = 0 , and execute this code.
[value1 && items.find(({value}) => value === value1).label,
value2 && items.find(({value}) => value === value2).label,
value3 && items.find(({value}) => value === value3).label,]
It returns result ["foo","bar",0] and 0 shows up on screen.
What I want is empty string instead of 0 like this ["foo","bar",""]
How I can do this?
You could take a default value.
const
items = [{ label: "foo" value: 1 }, { label: "bar" value: 2 }],
result = [1, 2, 3].map(v =>
v && state.items.find(({ value }) => value === v)?.label || '');
You could replace
value3 && items.find(({value}) => value === value3).label
with
value3 && (items.find(({value}) => value === value3).label ? 'your value' : '')

nested shape validation javascript

import get from "lodash.get";
const x = [
{
value: 1
},
{
value: {
min: undefined,
max: 2
}
}
];
console.log(
"valid: ",
x.every(o => o.value || (get(o, "value.min") && get(o, "value.max")))
);
https://codesandbox.io/s/modest-dijkstra-g42yy
I expect the valid to be false but it returned true although the value.min is undefined. What is the problem here?
The problem is that your first condition (o.value) returns true for the second item--since value is an object and therefore not falsy--so your check for min/max never runs.
const x = [
{
value: 1
},
{
value: {
min: undefined,
max: 2
}
}
];
// utility to check for null/undefined
const c = v => v != null;
console.log(x.every(({ value }) => (
typeof value === 'object'
? c(value.min) && c(value.max)
: c(value)
)));

Map through an inner array of an Object

I have this object:
let arr = [{
id : 1,
usr : 'pimba',
xyz: null
},
{
id : 2,
usr : 'aloha',
xyz: {
xyz_id: 2
}
},
{
id : 3,
age : 'pruu',
xyz: null
}];
As you can notice, sometimes xyz is null and sometimes it's not.
I need to recognize whether it is null or not, so I can read it.
I was trying to use map() function but I can't set some sort of filter to only execute the annonymous function when it is NOT null.
I managed to do something like this:
let result = Object.values(arr).map(function(row){
if(row['xyz'] != null) {
console.log(row['xyz_id']);
}
});
what If I want a new array containing ONLY xyz_id ? Is there a shorter version ?
Second case:
There are more than 1 value inside xyz and it's NOT "named".
let arr = [{
id : 1,
usr : 'pimba',
xyz: null
},
{
id : 2,
usr : 'aloha',
xyz: {
xyz_id: {"value1Here", "Value2Here"}
}
},
{
id : 3,
age : 'pruu',
xyz: null
}];
It seems you want to map the array only for the elements that have not-null xyz property. One option is using both .filter and .map methods. Another option is using the .reduce method:
let result = arr.reduce(function(ret, row) {
// assuming `xyz` can be only `null` or an object
if ( row.xyz !== null ) {
ret.push(row.xyz.xyz_id);
}
return ret;
}, []);
You might want to look at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
const notNull = arr.filter(elm => elm.xyz !== null);
var a = {one: 1, two: null, three: 3, four: true}
var y = []
let scan = (obj) => {
Object.keys(obj).forEach(x => {
if (obj[x] === null) {
console.log('Its null')
} else {
// Extend here to datatypes
y.push(obj[x])
}
});
}
scan(a)
console.log(y)

How do I insert more that one triple in a Marklogic JSON document?

I am trying to insert some triples, via Javascript (query console), into a JSON document of the form
declareUpdate();
xdmp.documentInsert('/aem/5/content/demo-spark/en_GB/automation_article.json',
{
"triple" : {
"subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
"predicate" : "https://content.ea.com/iri/author",
"object" : "jasonmoore"
},
"triple" : {
"subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
"predicate" : "https://content.ea.com/iri/id",
"object" : "automation_article2"
},
"triple" : {
"subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
"predicate" : "https://content.ea.com/iri/dateCreated",
"object" : "2015-08-14 09:38:10 GMT-7:00"
},
"content" : {
. . .
}
});
However, when I look in the newly created document, only the last triple is there, the other two are missing.
What do I need to do to get the first two triples in the same document?
I tried to add this as a comment, but it won't format it with line breaks. So this is just an extension of the answer from Jose Hermosilla Rodrigo.
Since you can't have many object keys with the same name, use an array:
declareUpdate();
xdmp.documentInsert('/aem/5/content/demo-spark/en_GB/automation_article.json',
{ "triples": [
{ "triple": {
"subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
"predicate" : "https://content.ea.com/iri/author",
"object" : "jasonmoore"
}},
{ "triple": {
"subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
"predicate" : "https://content.ea.com/iri/id",
"object" : "automation_article2"
}},
...
],
"content" : {
. . .
}
});
A JSON object stores key-value pairs. The keys are unique.
var obj = {
a : 'This is a property, but it will be overwritten',
a : 'Im really the value of a property'
};
console.log(obj);
That's the same to say :
var obj = {
a : 'This is a property, but it will be overwritten'
};
obj['a'] = 'Im really the value of a property';
console.log(obj);
Now you can think what's happening: Everytime you try to insert in the key "triple" is overwritting what it contains, and the value that finally stores is the last one.
var myDbObject = {};
var obj = {
"triple" : {
"subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
"predicate" : "https://content.ea.com/iri/author",
"object" : "jasonmoore"
},
"triple" : {
"subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
"predicate" : "https://content.ea.com/iri/id",
"object" : "automation_article2"
},
"triple" : {
"subject" : "https://content.ea.com/aem/5/content/demo-spark/en_GB/automation_article2.json",
"predicate" : "https://content.ea.com/iri/dateCreated",
"object" : "2015-08-14 09:38:10 GMT-7:00"
}
};
Object.keys(obj).forEach(key=>{
myDbObject[key] = obj[key];
});
console.log(myDbObject);

Categories

Resources