Javascript: change object key and value [duplicate] - javascript

This question already has answers here:
Changing the key name in an array of objects?
(11 answers)
Closed 2 years ago.
I have an object that I receive from an api, and each object's length are similarly long.
now, it does have a key fname and lname and I want it to become just name. I don't want to manually create an empty array and push a new one there just to change fname and lname into name.
Is there anyway I can achieve this without looping through the object and manually pushing it?

You can destructure the object while mapping it, here I have created a dummy data, let me know if this is something what you need:
var array=[{id:1, fname:'Bob', lname: 'Alice', someotherkey:'key'}];
var result = array.map(({fname, lname, ...rest})=>({name:fname+' '+lname, ...rest}));
console.log(result);

Related

"indefined selection console.log(data[])" [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 1 year ago.
I can recover my api but I cannot recover firstname in my object people, do you have a solution?
here my code for select l'object people :
console.log(data['people']);
and i want select firstname in my object people
Your people object is an array so, to get the firstname value you need to either loop in this array (with foreach or map) and then use .firstname, or you can just specify an entry
so you can do :
data['people'][0].firstname
or
data['people'].forEach(element => {
element.firstname
});
can you loop the array by using map.
data['people'].map((item, index) => {
console.log(item.firstname)
})

How to set the named index of associative array dynamically? [duplicate]

This question already has answers here:
JavaScript set object key by variable
(8 answers)
Closed 3 years ago.
I am trying to create an array with the following data as example. I want to set the named index dynamically based on the user selected value. But when I set the index to a variable, the declared variable name is considered a string in the array and set as the array index name.
var country = userInputFromUi;
var cities = [a,b,c,d];
array.push({
country:cities
})
Expected result
userInputFromUi:[a,b,c,d]
actual result
country:[a,b,c,d]
Not sure if I am understanding something wrong here.
Use computed property names like so:
array.push({ [country]: cities });

Why doesn't JavaScript convert a string to an Object? [duplicate]

This question already has answers here:
How to check if object property exists with a variable holding the property name?
(11 answers)
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I'm attempting to run a list of strings through an object. When I do it individually it works, but when I pass it through as a string it doesn't work. How would I fix this?
// this doesn't work
var a = "IntegrationItem1";
var data = faq.a;
// but this works
var data = faq.IntegrationItem1;
What's causing the first example to not work? Is the variable data seeing it as faq."IntegrationItem1" instead of faq.IntegrationItem1?
You can access properties of the object using it's names:
var a = "IntegrationItem1";
var data = faq[a];
what you need is faq["IntegrationItem1"] => faq[a]

how to add key and a value to a json with same existing Key using Javascript [duplicate]

This question already has answers here:
How to add new property with same key name inside declared object?
(3 answers)
Closed 6 years ago.
I have a json variable like this
var jsondata={"key1":"val1", "key2":"val2"}
I want to push another object with same existing key, and i want that my variable will be like this
var jsondata={"key1":"val1", "key2":"val2", "key1":"val3"}
I tried jsondata["key1"] = "val3", but it didn't return the wanted result
Thank you in advance.
you cannot, as it is a map.
but you could create this json :
var jsondata={"Name":["Jhon","James"], "Age":40}
You can't use the same key in an object. Your question suggests that the logic behind your data structure is wrong.
An alternative:
Use a different field name, i've used "_Name" below, but perhaps "Second_Name" would be more appropriate. Unsure what your json data is modelling.
var jsondata={"Name":"Jhon", "Age":40, "_Name":"James"};
Or perhaps it makes sense to store an array of people, is that what you're trying to achieve? i.e. you have two people, with the names "Jhon" and "James"?
var jsondata={
"people": [
{"Name":"Jhon", "Age":40},
{"Name":"James"}
]
};

How can I turn a string into a key? [duplicate]

This question already has answers here:
Javascript create variable from its name
(5 answers)
Closed 8 years ago.
I need to run this line of Javascript:
#model.save({ name: #some_element.val() })
But the key, which in this case is name, will change depending on the value of a variable. The variable is a string representation of the key. So in this case, the variable is "name". How can I use the variable to specify the correct key? If I use the variable name directly it is interpreted as the key itself.
var obj = {};
obj[varName] = #some_element.val();
#model.save(obj);

Categories