How to create a key value pair from two values [duplicate] - javascript

This question already has answers here:
How to convert an array into an object in javascript with mapped key-value pairs?
(4 answers)
Closed 7 months ago.
I'm returning key-value fields from the database, how do merge the two values into a new key-value pair?
{
"conditions": [{
"key": "target_condition",
"value": "100"
},
{
"key": "target_count_or_value",
"value": "value"
}
]
}
How to:
{
"conditions": {
"target_condition": "110",
"target_count_or_value": "quantity" }
}

data.conditions = data.conditions.map(item=>{[item.key]: item.value})
this uses the map function and dynamic object keys to build new objects

Related

How do I convert an object of objects to array of objects [duplicate]

This question already has answers here:
How to get all properties values of a JavaScript Object (without knowing the keys)?
(25 answers)
Closed 8 months ago.
I am fetching an API endpoint which returns data in the format
{
"0": {
"name": "Rohan"
},
"1": {
"name": "Meghan"
},
"2": {
"name": "Rita"
}
}
But since it is not array my map function throws an error
Home.jsx:39 Uncaught TypeError: users.map is not a function
Hence I want to convert this object of objects into a const arrOfarr. The array should look like:
[
{
"name": "Rohan"
},
{
"name": "Meghan"
},
{
"name": "Rita"
}
]
You can get key and value pairs with this line.
Object.entries(yourObj)

How can I remove objects from an array if a property is in another array? [duplicate]

This question already has answers here:
How can I loop through a JavaScript object array?
(13 answers)
Remove Object from Array using JavaScript
(32 answers)
How do I check if an array includes a value in JavaScript?
(60 answers)
Find object by id in an array of JavaScript objects
(36 answers)
Closed 11 months ago.
I want to slice an object from an array in React Native. Let's assume my object is like this:
const data = [
{"name": "alex", "subject": "english" "student_id": "1"},
{"name": "hales", "subject": "science" "student_id": "2"},
{"name": "joss", "subject": "english" "student_id": "3"},
{"name": "alexandra", "subject": "science" "student_id": "4"},
]
const ids = ["3", "4"];
I want to slice those array elements having student_id equal to ids

Get value from object/array [duplicate]

This question already has answers here:
JavaScript property access: dot notation vs. brackets?
(17 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 1 year ago.
I need to get the token only in a variable in JS.
{
"user": {
"id": "string",
"nickName": "string",
"score": 0,
"rank": 0
},
"token": "string"
}
This is my response saved in a variable but I only need to get the "String" value from the token
If you have stored this response in a variable E.g.:
let response = {
"user": {
"id": "string",
"nickName": "string",
"score": 0,
"rank": 0
},
"token": "string"
}
You can extract the value from the "token" property like this
let tokenFromObject = response.token
or
let tokenFromObject = response["token"]
or
let { token } = response
You should be able to take the variable you got this json in and do this:
let a = {"user":{},"token": "string"}; // this is what you got
console.log(a["token"]) // a["token"] will give you "string"

I am trying to find a specific object in an array using node.js [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 2 years ago.
My array is:
[
{
"_attributes": {
"key": "attributes"
},
"dt_assoc": {
"item": {
"_attributes": {
"key": "status"
},
"_text": "taken"
}
}
}
]
I have tried a couple of options but can't seem to get a specific object. Im trying to get; "dt_assoc":
{
"item": {
"_attributes": {
"key": "status"
},
"_text": "taken"
};
That's an array with only one item. Counting starts at zero. So to get to the item, you'd use array[0].
That will get you the object. That object has a property named dt_assoc. To access the property, you'd use array[0].dt_assoc.
You access it by specifying index as 0 because it is an array.
const data = [{
"_attributes": {
"key": "attributes"
},
"dt_assoc": {
"item": {
"_attributes": {
"key": "status"
},
"_text": "taken"
}
}
}]
console.log(data[0].dt_assoc)

see undefined when try to parse json [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
I have tried to parse this JSON file. But I see undefined.
I need to receive only value, where the key equals level1.
[{
"id": 2,
"name": "Peter",
"products": [{
"title": "first",
"price": 100
},
{
"title": "second",
"price": 200,
"desciption": [{
"level1": "good",
"level2": "bad"
},
{
"level3": "super",
"level4": "hell"
}
]
}
],
"country": "USA"
}]
const fs = require('fs');
let file = fs.readFileSync("./file.json");
let parsed = JSON.parse(file);
console.log(parsed["name"])
console.log(parsed.name);
and I see in the conlose "undefined"
Your JSON data represents an array of objects. If after parsing you want the property "name" of the first element, it's:
console.log(parsed[0]["name"])
or
console.log(parsed[0].name);

Categories