How does destructuring assignment evaluate the result in my case? - javascript

I have following javascript code,
op = [
{busiCode:"001",
busiName:"Order"
},
{busiCode:"002",
busiName:"Plan"
}
]
const [{ busiCode = '' } = {}] = op
console.log(busiCode)
It prints 001, I would ask how javascript evaluate the reuslt in the above code,
Thanks!

The source value is an array. Thus, in the const declaration, the first [ says "look inside the array"
const [ // plz look in array
Then, in the source, the elements of the array (well, the first one at least), are objects. Thus in the declaration the first { means "in the first element of the array, treat it as an object and look inside".
const [{ // plz look in object at array[0]
Then, the identifier busiCode says that you want it to look for a property called "busiCode" in the object in the array, and that you want a local constant with that name ("busiCode") and the value of that property in that first object in the array.
const [{ busiCode // plz get "busiCode" property
Then the = '' means "if you can't find busiCode as a property just give me an empty string".
const [{ busiCode = '' // default to empty string if not found
Then the object "lookup" part is completed with }, and = {} means that if the original array has no first element, do all that stuff with a dummy empty object".
const [{ busiCode = '' } = {} // give me at least an empty string
The pattern of the left side of the destructuring matches the general "shape" of the right-hand side.

Related

How to grab the children of an Object.keys(myEl) reference

In the code below, I can get a reference to the text000 object, but I need to capture its child array as my target payload. Once I have a reference to the key, how can I capture its children?
Full object is below:
activeItem = [{"dnd":{"index":0,"active":true,"group":"common","label":"Text (000)","type":"text"},
"json":{"schema":{"properties":{"text000":{"title":"Text (000)","type":"string"}},"required":["text000"]},"layout":[{"key":"text000","description":"","floatLabel":"auto","validationMessages":{"required":"Required"}}]}}]
To grab a reference to the "text000" key I'm using:
const myEl = Object.keys(this.activeItem.json.schema.properties); // points to text000
I need to pull that key's contents/children > {"title":"Text (000)","type":"string"} out to use it as my target payload for this operation.
The text000 element is dynamic so I need its reference, which is why I'm using the Object.keys() method to point to it.
Feel free to school me on the proper names to use to refer to these elements. For example, not sure exactly how to reference > {"title":"Text (000)","type":"string"} with respect to the key text000. Is that the key's "children", "value", "contents" or what?
UPDATE:
console.log('TRY: ', this.activeItem.json.schema.properties[0]);
// Returns undefined
console.log('TRY2: ', this.activeItem.json.schema.properties);
// Returns {"text000":{"title":"Text (000)","type":"string"}}
I need something to return:
{"title":"Text (000)","type":"string"}
SOLUTION thanks #jaredgorski:
const properties = this.activeItem.json.schema.properties;
const propertiesKeys = Object.keys(properties);
const propertiesKeysFirstVal = Object.keys(properties)[0];
const logProperties = properties[propertiesKeysFirstVal];
console.log('PROPERTIES KEYS:', propertiesKeys);
console.log(
'VALUES OF FIRST PROPERTIES KEY:',
propertiesKeysFirstVal
);
console.log('RESULT:', logProperties);
PROPERTIES KEYS: ["text000"]
wrux-wrux-form-builder.js:1782 VALUES OF FIRST PROPERTIES KEY: text000
wrux-wrux-form-builder.js:1783 RESULT: {title: "Text (000)", type: "string"}
You need to remember that activeItem is an array. As long as you include the index (in this case the first index, which is [0]), you can access the json property (or key) and continue down the chain to retrieve the values in text000.
The other trick here is that you're wanting to access the first key in properties, but you don't know the name of that key yet. So what you need to do is actually make an array of the keys and then find out the name of the first key in that properties object. To do this, you can use Object.keys(), a method which turns the keys of an object into an array. Once you have the name of this key, you only need to use bracket notation on the properties object to find the value for this key. I'll show you how this works in the snippet below.
Here are some references so that you can learn more about how this works:
MDN page on the Object.keys() method
Accessing JavaScript
object properties: Bracket notation vs. Dot notation
And here's the working example:
const activeItem = [
{
"dnd": {
"index": 0,
"active": true,
"group":"common",
"label":"Text (000)",
"type":"text",
"icon":"text_fields",
"fontSet":"material-icons",
"class":""
},
"json": {
"schema": {
"properties": {
"text000":{
"title":"Text (000)",
"type":"string"
}
},
"required":["text000"]
},
"layout":[
{
"key":"text000",
"description":"",
"floatLabel":"auto",
"validationMessages": {
"required":"Required"
}
}
]
}
}
]
// This is the dirty looking version:
const logPropertiesDirty = activeItem[0].json.schema.properties[Object.keys(activeItem[0].json.schema.properties)[0]]
console.log("First, the dirty version where we don't save anything to variables. Everything is laid out here.")
console.log('WHAT WE DID:', 'activeItem[0].json.schema.properties[Object.keys(activeItem[0].json.schema.properties)[0]]')
console.log('RESULT:', logPropertiesDirty)
console.log('=================================================')
// This is the cleaner version, using variables to store things as we go:
const properties = activeItem[0].json.schema.properties;
const propertiesKeys = Object.keys(properties);
const propertiesKeysFirstVal = Object.keys(properties)[0];
const logPropertiesClean = properties[propertiesKeysFirstVal];
console.log('Now, the cleaner version. We save some values to variables to make things more readable.')
console.log('PROPERTIES OBJECT:', properties)
console.log('PROPERTIES KEYS:', propertiesKeys)
console.log('NAME OF FIRST PROPERTIES KEY:', propertiesKeysFirstVal)
console.log('RESULT:', logPropertiesClean)
Regarding what to call these things, I've always thought of Objects as generally consisting of "key-value pairs". Keys can also be called properties and values can also be called contents (I guess).
myObject = {
key1: value1,
property2: contentsOfProperty2
}
At the end of the day, clear communication is all that counts! So, whatever names you come up with (as long as they make reasonable sense), I'm sure people won't be jerks about it unless they feel like they have something to prove.
You should be able to use Object.values over this.activeItem.json.schema.properties:
The Object.values() method returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).
const object1 = {
a: 'somestring',
b: 42,
c: false
};
console.log(Object.values(object1));
// expected output: Array ["somestring", 42, false]
It is not supported across the map yet, but you should be able to load a polyfill if you need it.

chineseFood[array[0]] = array[array.length-1];

I don't understand the purpose of this = sign on the sixth line in the code block below. I understand how the argument grabs each index number of the array, I just don't understand why chineseFood[array[0]] = array[array.length-1]; In other words, I don't get the purpose of the equal sign as if it were almost comparing each other to be stored in the empty object that is stored in the variable chineseFood. Could someone please clarify? It would be much appreciated.
function transformFirstAndLast(array) {
var chineseFood = {};
//takes 1st element (at index 0) and sets it to the last element (nth index): array(length-1)
chineseFood[array[0]] = array[array.length - 1];
return chineseFood;
}
console.log( transformFirstAndLast(['Orange', 'Lemon', 'Pork', 'Chicken']) );
Output Below
{Orange: "Chicken"}
The equals sign is not comparison, it is assignment. chineseFood is an object, which means that it can be treated like a dictionary, and its properties can be accessed using the [] operator instead of the . operator:
myObj = {
foo: "bar"
};
console.log(myObj["foo"]); // bar
console.log(myObj.foo); // bar
Likewise, you can also assign properties this way:
myObj = {};
myObj["foo"] = 3;
console.log(myObj["foo"]); // 3
console.log(myObj.foo); // 3
This is what your code is doing. It is retrieving the value of array[array.length-1], which is "Chicken". Then it is assigning this value to the property of chineseFood that has the name represented by array[0], which happens to be "Orange". Thus, the property named Orange on chineseFood is set to array[array.length - 1], which is why chineseFood evaluates to {Orange: "Chicken"}.
This method of accessing properties is especially useful when you don't know the name of the property you will be changing in advance, as is the case with this code, or when you want to create properties that have names that would otherwise be illegal:
myObj = {
".you can't usually use with spaces or start w/ periods": false
};
myObj[".you can't usually use with spaces or start w/ periods"] = true;
console.log(myObj[".you can't usually use with spaces or start w/ periods"]);
// there is no way to read this property the normal way
Basically what is does is:
your object is :
var obj = {Orange: "Chicken"};
And Your array is :
var arr = ['Orange','Lemon','Pork','Chicken']
What this line says is pick first element of the array and check for this prop in object and change its value to last element of array, here:
arr[0] = "orange";
So this line :
obj[arr[0]] can be seen as obj['orange'].
After that you change its value:
Obj[arr[0]] = arr[arr.length-1] which can be written as obj['orange'] = 'chicken'

find if path exists in json nodejs

I would like to add to the an existing json a new value in the following path:
 
VAL:"BS", PATH:["info", "bosses", "lives"]
if my json had the passes it will add the value, otherwise I will create the fields
example:
var myJson = {
    "root": {
        "name": "jim",
        "age": "4",
        "info": {"bosses": {"name": "sam"}}
    }
}
so my new json will look like this:
var myNewJson = {
    "root": {
        "name": "jim",
        "age": "4",
        "info": {"bosses": {"name": "sam", "lives": "BS"}}
    }
}
an example when I didn't have the fields:
var myJson = {
    "root": {
        "name": "jim",
        "age": "4",
    }
}
the output:
var myNewJson = {
    "root": {
        "name": "jim",
        "age": "4",
        "info": {"bosses": {"lives": "BS"}}
    }
}
an example where part of the path exists I didn't have the fields:
var myJson = {
    "root": {
        "name": "jim",
        "age": "4",
 "info": {"salary": 500}
    }
}
the output:
var myNewJson = {
    "root": {
        "name": "jim",
        "age": "4",
        "info": {"salary": 500, "bosses": {"lives": "BS"}}
    }
}
how can I check if path exists (note: part of the path might exist- how can I know from where to start it?)
This is old, but...
testing existence of fields on paths is nicer now, with optional chaining, like:
var infoExists = !!myJson?.root?.info - the ?. means that if root were missing, the line wouldn't break.
In this case though, I wouldn't worry about testing for the existence of fields, I'd use spread operators to just rebuild the info object:
var newInfo = {"bosses": {"lives": "BS"}};
var info = {...myJson.root.info, ...newInfo};
if myJson.root.info doesn't exist, no problem (as long as myJson.root does, cue an optional chaining check?), we just get the newInfo.
if myJson.root.info currently holds {"salary": 500}, the spread operators will combine that to "info":{"salary": 500, "lives": "BS"}.
if myJson.root.info currently holds the same as newInfo, well, no sweat, we end up with no change.
Now you have the info object exactly as you want it, you can simply replace it in the original object with
myJson.root.info = ...info;
The first thing you could think it works could be some code like:
if (myObject.attributeName){
//property exists!
}
It will work for you in many tests but I sure you know that javascript manage some values as truthy or falsy. It use lot's of different type value as boolean false in many comparison (just === not convert type).
When you check something as
if(somethingtotest){
// it's true
}
It is the same you'd write
if(somethingtotest == true){
// it's true
}
'==' operator tryes to convert different types to be "similar" together.
In the first example code many of object attribute values can be '==' true.
That attribute value is truthy, meaning it’s an object, a non-empty string, a non-zero number that’s not NaN, true, and not null or undefined.
That means if the attribute is an empty string (“”), this check will fail. The same if value is 0 (zero), null, false. Failing, in this case, doesn’t mean that the property doesn’t exist. In fact, the property does exist and contains a value, but the value is falsy and so doesn’t pass this test.
Since ECMAScript 5 the base Object has the function hasOwnProperty(propertyName)
So you could try
if(myObject.hasOwnPorperty('attributeName')){
// do something
}
This function remembers us that the Objects has also prototype attributes that our object could inherit. For example if I write
myObject.anyAttribute='AnyValue';
var my1stObject = Object.create(myObject);
var my2ndObject = Object.create(myObject);
myObject.anyAttribute='AnotherValue';
console.log(my1stObject.anyAttribute);
console.log(my2ndObject.anyAttribute);
last two rows will print 'AnotherAttribute'.
In this case 1st and 2nd objects have not own property anyAttribute but they will use the prototipe one.
Different if you write
myObject.anyAttribute='AnyValue';
var my1stObject = Object.create(myObject);
var my2ndObject = Object.create(myObject);
my1stObject.anyAttribute='AnotherValue';
console.log(my1stObject.anyAttribute); //AnotherValue
console.log(my2ndObject.anyAttribute); //AnyValue
First object hasOwnProperty 'anyAttribute' because we set it.
At last, to be shure to check if property, proptotipe or own, really exists, you would like to use
if('anyProperty' in myObject){
//do something
}
I suggest you to read it better at this link:
https://www.nczonline.net/blog/2010/07/27/determining-if-an-object-property-exists/

Difference between [][[]] and [[]][] in JavaScript?

The result of [ ][[ ]] is undefined
where as this [[ ]][ ] throws SyntaxError: Unexpected token ].
Any Explanation?
The object[key] syntax is used to get the property of the object by the key, which must have key inside [] otherwise there will be syntax error.
[][[]], the object is an empty array [], and the key is another empty array [].
[[]][], the object is an array with an empty array inside it, while there is no key inside [].
[] defines an empty array, so [][[]] could be rewritten as:
var a = [];
var b = [];
var c = a[b];
Now accessing an array by index where the index itself is an array is undefined - the standard doesn't say what should happen, but it's syntactically valid.
However [[]][] is broken - the closest this can be written longhand is:
var a = [];
var b = [];
[b]a;
...which is nonsense code.
I can't see why anyone would ever want to use something like this, but here is an explanation:
Case 1:
[] is an empty array. By adding [n] to it ([][n]), you reference the n-th element. n in your case is []. The interpreter tries to convert that to a usable value and ends up with an empty string (""). So you actually try to reference [][""] which is undefined.
Case 2:
[[]] is an empty array inside an empty array. By adding [n] to it ([[]][n]), you reference the n-th element of the outer array. You need to provide the index n, otherwise it throws an error, which happens in your case.
I can't imagine why you would need to use this, but here is my explanation:
[[]] is new Array( new Array()), which is an array with ONE element, an empty array.
[] is an empty array, so you can do [][2], or [][0] and it returns undefined. The second [] is the index.
So in [] [[]] you are looking for an index, the index [].
[][0] == [][[]]
But in [[]] [] the index is empty, is equivalent to [1,2,3][] -> SyntaxError: Unexpected token ]
ok. simply you should access array with a key. else it will throw syntax error.
[][[]] - array defined and you are accessing key [] from that array which is undefined.
[[]][] - array defined and you are not passing any key to access from that array - syntax error.
I guess a type conversion occurs :
[][
// an index is expected here
[] // so javascript probably tries to get a number from this empty array
]
This might be equivalent to :
[][+[]] // +[] gives 0
While in this pattern [[]][] two arrays are declared side by side.
A JavaScript object has properties associated with it. You access the properties of an object with a simple dot-notation or bracket notation, and if non existing key refered it gives undefined value.
var obj = {};
obj[ 'key' ]
and one important thing is that in JavaScript array is a Object
console.log( [] instanceof Object ) // gives true so it act like object
and funny thing is that we pass like below .
var keyCanbeFunction = function(){};
var keyCanbeObject = {};
var keyCanbeArray = [] ;
console.log( obj[ keyCanbeArray ] ) // gives undefined, that key not exists
and 1 st case :
var obj = [];
obj[ keyCanbeArray ] // gives undefined
and 2nd case :
var obj = [[ ]]
obj[]; // where is key ???

how to declare an associative array in javascript? Unlike PHP, I find it really difficult to implement

How do you access a key named "foo" of the 5th element of an array that is the 2nd element of another array that is the value of a hash key named "baz"?
can you provide the basic examples?
You want to use an object:
var obj = {
foo: {
bar: "yo"
}
}
console.log(obj["foo"]["bar"]);
// Or you could do this
var obj = {};
obj["foo"] = {};
obj["foo"]["bar"] = "yo";
console.log(obj["foo"]["bar"]);
Note the order of an object is not guaranteed and can change depending on the interpreter / compiler.
An array in JavaScript is just an object with numerical indices (With some specific methods - see comment below). In fact pretty much everything in JavaScript is an object.
How do you access a key named "foo" of the 5th element of an array
that is the 2nd element of another array that is the value of a hash
key named "baz"?
variableName["baz"][1][4]["foo"];
Would get you "The value here" from
var variableName = { baz: [0, [0, 1, 2, 3, { foo: "The value here" }]] };
and here's a jsFiddle to play with

Categories

Resources