where is stored the property "cle" of an object created with for example "const o = Object.create({cle:"valeur"})"? - object

When we create an object "o" with the following syntax [ const o = Object.create({cle:"valeur"}) ] , where is stored and hidden the property "cle" ?
It is accessible with only [ o.cle ]
and with none of the following commands :
How can we list the property "cle" or else ?
const o = Object.create({cle:"valeur"});
console.log(o.cle); // valeur
console.log(Object.entries(o));
// [] length:0 [[Prototype]]:Array(0)
console.log(Object.getOwnPropertyDescriptors(o));
// {} [[Prototype]]:Object
console.log(Object.getOwnPropertyDescriptor(o,"cle"));
// undefined
console.log(Object.getOwnPropertyNames(o));
// [] length:0 [[Prototype]]:Array(0)
console.log(Object.hasOwnProperty(o,"cle"));
// false
console.log(Object.hasOwn(o,"cle"));
// false
console.log(o.prototype); // undefined
console.log(Object.prototype);
// no "cle"

Related

How to print the object value which return type is [Object , Object] in Nod JS?

below is one piece of code where i have to compare one object stored value (i.e 'resort') with the value compare that value what i am getting from the JSON file .
code -
resort = _.find(this.resorts.entries, (o) => {
return o.gqe_name === resort;
})
;
i have tried to get the value but the it is displaying as [Object,Object ] , tried with console .log('resort'+ resort) and log.info ('resort'+ resort).
is there any way i can view the return value ?
how i can print json stored value 'o.gqe_name' ?
JSON.stringify can help
const object = { test: { test2: 'value' } }
const result = JSON.stringify(object)
const result2 = JSON.stringify(object, null, 2)
console.log(result)
console.log(result2)

how to check if one of the array got a value using Ramda

hi im trying to check if one of the array got a value and it need to return true
input1 = { "value": [
{
"props": {
"forest": []
}
},
{
"props": {
"forest": [
{
"items": "woods"
}
]
}
} ] }
input2 = { "value": [
{
"props": {
"forest": []
}
},
{
"props": {
"forest": []
}
} ] }
the code i tried is R.anyPass to check if one of the value is True and if yes then it return true
const forestgotwoods = R.pipe(
R.path(['value']),
R.map(R.pipe(
R.path(['props','forest']),
R.isEmpty,
R.not,
)),
R.anyPass
);
console.log(forestwoods(input1)); undef
console.log(forestwoods(input2)); undef
and I also try it this way
const forestgotwoods = R.anyPass(
[R.pipe(
R.path(['value']),
R.map(R.pipe(
R.path(['props','forest']),
R.isEmpty,
R.not,
))
)]
);
console.log(forestwoods(input1)); //true
console.log(forestwoods(input2)); //true
The result for input1 need to be true
The result for input2 need to be false
Use R.any that returns a boolean according to the predicate. R.anyPass accepts an array of predicates, which is not needed here. You can remove the R.anyPass from the start because we don't need the check here.
In addition, you need to check if any any array of props.forest is empty, so remove replace R.map with R.any.
const forestgotwoods = R.pipe(
R.prop('value'), // get the value array
R.any(R.pipe( // if predicate returns true break and return true, if not return false
R.path(['props', 'forest']), // get the forest array
R.isEmpty,
R.not,
))
)
const input1 = {"value":[{"props":{"forest":[]}},{"props":{"forest":[{"items":"woods"}]}}]}
const input2 = {"value":[{"props":{"forest":[]}},{"props":{"forest":[]}}]}
console.log(forestgotwoods(input1)); // true
console.log(forestgotwoods(input2)); // false
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
Another option is to use R.all to check if all are empty, and then use R.not to negate the result of R.all:
const forestgotwoods = R.pipe(
R.prop('value'), // get the value array
R.all(R.pipe( // if predicate returns true for all, return true, if at least one returns false, return false
R.path(['props', 'forest']), // get the forest array
R.isEmpty,
)),
R.not // negate the result of R.all
)
const input1 = {"value":[{"props":{"forest":[]}},{"props":{"forest":[{"items":"woods"}]}}]}
const input2 = {"value":[{"props":{"forest":[]}},{"props":{"forest":[]}}]}
console.log(forestgotwoods(input1)); // true
console.log(forestgotwoods(input2)); // false
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
Using my 2nd solution with R.propSatisfies (suggested by #Hitmands) generates a short and readable solution:
const forestgotwoods = R.pipe(
R.prop('value'), // get the value array
R.all(R.pathSatisfies(R.isEmpty, ['props', 'forest'])), // if all are empty, return true, if at least one return is not, return false
R.not // negate the result of R.all
)
const input1 = {"value":[{"props":{"forest":[]}},{"props":{"forest":[{"items":"woods"}]}}]}
const input2 = {"value":[{"props":{"forest":[]}},{"props":{"forest":[]}}]}
console.log(forestgotwoods(input1)); // true
console.log(forestgotwoods(input2)); // false
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
I think this is a simpler, more readable Ramda approach:
const forestHasWoods = where ({
value: any (hasPath (['props', 'forest', 0]))
})
const input1 = {value: [{props: {forest: []}}, {props: {forest: [{items: "woods"}]}}]};
const input2 = {value: [{props: {forest: []}}, {props: {forest: []}}]};
console.log (forestHasWoods (input1))
console.log (forestHasWoods (input2))
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
<script> const {where, any, hasPath} = R </script>
where is used to turn a description of an object into a predicate, especially useful in filtering. any has been discussed in other answers, and is definitely what you want here rather than anyPass. And hasPath reports whether three is a value to be found at this path in the given object..
There is a potential issue here, I suppose. If you are dealing with sparse arrays -- well if you are, you're already in a state of sin -- but if you are, then forest could have a value but not one at index 0. If this is the case, you might prefer a version like this:
const forestHasWoods = where ({
value: any (pathSatisfies (complement (isEmpty), ['props', 'forest']))
})
Similarly to the previous answer from #Ori Drori,
you could also leverage R.useWith to create a function that operates on both arrays...
const whereForestNotEmpty = R.pathSatisfies(
R.complement(R.isEmpty),
['props', 'forest'],
);
const findForest = R.pipe(
R.propOr([], 'value'),
R.find(whereForestNotEmpty),
);
const find = R.useWith(R.or, [findForest, findForest]);
// ===
const a = {
"value": [
{"props":{"forest":[]}},
{"props":{"forest":[{"items":"woods"}]}}
]
}
const b = {
"value": [
{"props":{"forest":[]}},
{"props":{"forest":[]}}
]
}
console.log(find(a, b));
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
Note: use R.complement to negate predicates

how to remove {} empty field in array .. nodejs

i am using below code...but if Status is Empty iss show below result in database...i want to remove if Status is Empty ..
1 Address {}, Status {} save in database.
function CheckStatus(oldJob, newJob) {
var obj = {};
if(newJob && newJob.Status) {
obj.Status= {};
if (oldJob.Status.total !== newJob.Status.total) {
obj.Status.total = newJob.Status.total;
}
if (oldJob.Status.charge_description && oldJob.Status.charge_description !== newJob.Status.charge_description) {
obj.Status.charge_description = newJob.Status.charge_description;
}
}
}
Mongodb
"Job" : {
"_id" : ObjectId("5873b352e7621d08fccc5890"),
"updated_by" : "",
"details" : "{\"current_status\":\"Completed\",\"address\":{},\"Status\":{}}",
"changetype" : "Update",
"datetime" : ISODate("2017-01-09T15:59:14.202Z")
},
please help how what enter in If Condition ( below code not working)
if(obj.address = '{}')
{
console.log('Empty');
}
Setting an object's value to undefined essentially deletes them. You may use the undefined keyword easily as such:
newJob.address = undefined;
How i would check for an empty object and delete its content:
if(Object.keys(newJob.address).length === 0 && obj.constructor === Object){
newJob.address = undefined;
}
Solution is better explained in this question's answers:
How do I test for an empty JavaScript object?

Prevent global, process, GLOBAL and root parameters from being iterated in a for loop

I have a function on one of my express application modules to which a parameter page is passed :
module.exports.savePage = function (page){
console.log('-- Running M2PagePers.savePage.. ');
// il faut prévoir que quelqu'un utilise M2Page.save() pour un update
for(var prop in page) {
if(typeof(page[prop]) != 'function' && page.hasOwnProperty(prop))
console.log('page['+ prop +'] : ' + page[prop] + ' propertyIsEnumerable ? ' + page.propertyIsEnumerable(prop));
};
};
but i got some more properties in the page parameter which i didn't expect to see which are global, process, GLOBAL and root:
page[global] : [object global] propertyIsEnumerable ? true
page[process] : [object process] propertyIsEnumerable ? true
page[GLOBAL] : [object global] propertyIsEnumerable ? true
page[root] : [object global] propertyIsEnumerable ? true
page[console] : [object Object] propertyIsEnumerable ? true
page[url] : urltest1 propertyIsEnumerable ? true
page[type] : typetest1 propertyIsEnumerable ? true
page[definition] : deftest1 propertyIsEnumerable ? true
page[nb_visiteurs] : 656555 propertyIsEnumerable ? true
I don't really care from where they come (i guess they are some node.js global variables..) but even when i tried to make tests with hasOwnProperty and propertyIsEnumerable to filter the parameters iterated on the for loop , they are still there
--- EDIT ----
this is the index.js in which i defined a class M2Page, passing the page parameter occurs inside the savePage function by passing this to M2PagePers.savePage(this);
function M2Page (page, perspage) {
this.url= null;
this.type= null;
this.definition= null; // [sect1, sect2, ... ,sectN]
this.nb_visiteurs= null;
this.droits_acces= null;
this.secu_params= null;
this.content= null;
this.apparence=null;
// permet d'initialiser la page avec page_props dans le constructeur
flow.exec(
function(){
console.log('running flow m fn 1..');
if(page != null && page != undefined)
(function (for_in_loop_cb) {
for(var prop in page) {
console.log('inside for in : setting this page\'s ' + prop + ' to ' + page[prop]);
this[prop] = page[prop];
};
for_in_loop_cb();
})(this);
},function(){
console.log('running flow m fn 2.. ');
if(perspage) savePage();
}
);
//fonctions de persistance
function savePage () {
M2PagePers.savePage(this);
};

Unable to get value of the property 'split': object is null or undefined

Newbie to HTML here. Using IE9, I'm getting the Error on Page Unable to get value of the property 'split': object is null or undefined. The selected code section is
var retDate = null
var oscriptDateString = form._1_1_56_1.value
if ( oscriptDateString != '?' )
{
var temp = oscriptDateString.split( '/' )
var temp2 = temp[ 3 ].split( ':' ) //Getting Error Here
var yearX = parseInt( temp[ 1 ] )
var monthX = parseInt( temp[ 2 ] ) - 1
var dayX = parseInt( temp2[ 0 ] )
var hourX = parseInt( temp2[ 1 ] )
var minuteX = parseInt( temp2[ 2 ] )
var secondX = parseInt( temp2[ 3 ] )
retDate = new Date( yearX, monthX, dayX, hourX, minuteX, secondX )
}
return retDate
It only returns this error when the field is blank. Otherwise works fine. Am I missing something?
You are reading from a field called _1_1_56_1
var oscriptDateString = form._1_1_56_1.value
You then split this field, and name the result temp
var temp = oscriptDateString.split( '/' )
If your temp is empty/null/undefined, because the above assignment failed (because the field was empty or it contained no slash...
temp[ 3 ].split( ':' )
Then temp here will be undefined. You can't call the fourth element (third position) of nothing.

Resources