Value of function parameter is not getting [duplicate] - javascript

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 3 years ago.
I'm not getting the actual value of my parameter name in the below function. Object.assign taking the function parameter name as a string literal so the resulted JSON object also named as name
see the below code.
Please see the resulted json object i got.
How to fix this?

wrap it with []
return Object.assign(..., {[name]: JSON.parse(...)})

Related

JavaScript object like String literal doesn't to set property [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
How to create an object property from a variable value in JavaScript? [duplicate]
(9 answers)
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 2 years ago.
In JavaScript everything is object like String literal, numbers, Object...
And properties can be added to objects dynamically also, but I am adding property to object which refer to string then it seems it is not working
Following is JS code which I am running
var aVar = "some string";
console.log(aVar);
aVar.name = 'raj';
console.log(aVar.name);
Output...
hi
some string
undefined

How to make string to object or array format [duplicate]

This question already has answers here:
Access a nested property with a string [duplicate]
(5 answers)
Get global variable dynamically by name string in JavaScript
(6 answers)
Closed 4 years ago.
I have to create a string in format like below
"currentState[0]['children'][1]"
But I need to execute it later just like below
currentState[0]['children'][1]
I have elements and childrens on currentState. But while looping I have to create a string. But later I need to execute as array.
I have tried almost all array methods. Array.call, bind etc. And string methods as well. Could not get the output
How can I make it
Please be more specific with your question but from my understanding, you can use javascript's eval() function to execute a string as javascript, so when you need to execute it, just run eval("currentState[0]['children'][1]").
The alternative to the eval() would be
function evalFn(obj) {
return Function('"use strict";return (' + obj + ')')();
}
evalFn("currentState[0]['children'][1]")
refer to: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval for a more in-depth explanation.

Assigning the value of a javascript variable within function arguments? [duplicate]

This question already has answers here:
Javascript - String concatenation [duplicate]
(2 answers)
Closed 6 years ago.
I'm sending an XMLHttpRequest.
In the open function I want to set some parameters for the request.
req.open('GET', /some/path?someparam=*somevariable*, true)
I want to get someparam to equal the value of a variable, like var somevariable. In fact the variable name might even have the same name as someparam.
How do I get the variable to resolve in this instance?
String concatenation and encodeURIComponent to make sure you are producing a valid URI:
'/some/path?someparam=' + encodeURIComponent(somevariable)
In an ES6 environment you can use template literals:
`/some/path?someparam=${encodeURIComponent(somevariable)}`

Updating object values in JavaScript via function [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 9 years ago.
In JavaScript I am trying to update an object value through a function, through which I am passing the object property to update.
However, this won't work - and I can see why, but don't know how to combat it!
myObject = {"testItem": "testValue"};
console.log(myObject.testItem);
function updateSomeValue(objectItem, newValue){
myObject.objectItem = newValue;
}
updateSomeValue('testItem', 'newValue');
console.log(myObject.testItem);
Now, I can see the issue here is that in the function, myObject.objectItem is expecting an item in the object called objectItem - it won't translate it to testItem.
How do I do this?
By using a different notation. Using [ .. ] you can specify the property name as a string.
function updateSomeValue(objectItem, newValue){
myObject[objectItem] = newValue;
}

test for a propertie in JSON project to be empty? [duplicate]

This question already has answers here:
Testing for an empty array object in JSON with jQuery
(5 answers)
Closed 8 years ago.
I have the following JSON object that is passed to one of my functions …
{"action":"setProjectAll", "application":{"proId":"new","zoomPosition":35,"scrollerPosition":0,"language":"en","timelinePosition":0}, "clips":[]}
How can I test this object for the propertie "clip" to be "[]" (empty)?
Right now in the example above it is empty, however since the object is dynamic I need to test for that property if it contains values or not.
How can that be done?
thank you
How about
if (x.clips && x.clips.length === 0)

Categories

Resources