This question already has answers here:
How do I remove all null and empty string values from an object? [duplicate]
(13 answers)
Closed 1 year ago.
I have a very large array of objects and some of the objects have a nameless property. What is an efficient way to remove the property?
[
...
{
"phone_number": "***-***-****",
"category": "Abc",
"link_to_company": "www.example.com",
"email": "test#test.com",
"company_name": "test company",
"": "123", // this need to be removed
"status": 1
},
...
]
A map function should do it.
array.map(v => { delete v[""]; return v })
Related
This question already has answers here:
Find object by id in an array of JavaScript objects
(36 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 months ago.
This post was edited and submitted for review 4 months ago and failed to reopen the post:
Original close reason(s) were not resolved
I add a json value from a response to a variable and then print it this way result: ${JSON.stringify(result)}, but what i want exactly is to get only one exact value from this json
here's the json
{
"flags": [
{ "flagID": "0f0571b5-af7b-48e2-8418-044cbec0166d", "flagname": "lionsflag", "team": "lions", "membersnum": "4", "win": false },
{ "flags": "9819c0bd-e134-4950-a1ee-7f89450ee4b6", "flagname": "Barcaflag", "membersnum": "4", "win": true }
]
}
I only want to extract lionsflag string only from the first flagname, this value is able to change any time
You need to use Object.keys() to get all the keys and then check the related value is meet your demand or not
let data = {
"flags": [
{ "flagID": "0f0571b5-af7b-48e2-8418-044cbec0166d", "flagname": "lionsflag", "team": "lions", "membersnum": "4", "win": false },
{ "flags": "9819c0bd-e134-4950-a1ee-7f89450ee4b6", "flagname": "Barcaflag", "membersnum": "4", "win": true }
]
}
let value = "lionsflag"
data.flags.filter(d => Object.keys(d).forEach(e =>{
if(d[e] == value){
console.log(e + ";" + value)
}
}))
let prop ='flagname'
console.log(data.flags[0][prop])
This question already has answers here:
Perform .join on value in array of objects
(14 answers)
Closed 1 year ago.
I need help in getting values of specific property of an array of multiple objects
I have an array like this:
[
{
"id": 1,
"name": "skill 1"
},
{
"id": 2,
"name": "skill 2"
},
{
"id": 3,
"name": "skill 3"
}]
and I want to get a string like this :
skill 1 - skill 2 - skill 3
First step is to extract name. You can use map.
elements.map(x=>x.name)
Second step is to join it to one string using join
const result = elements.map(x=>x.name).join(' - ');
This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 2 years ago.
I’d like to know if it is possible to create a function that returns a field from an object given in parameter.
const data = {
"name": "Doe",
"firstName": "John",
"age": 30,
"from": "USA"
};
function returnField(field) {
return data.field;
}
yes, its possible by using [] instead of .
data = {
"name" : "doe",
"firstname" : "jhon",
"age" : 30,
"from" : usa
}
function returnField(field){
return data[field]
}
In general you dont even need the function for it (unless you want to have more logic or some validation there) and just using data[field] directly.
This question already has answers here:
Access a nested property with a string [duplicate]
(5 answers)
Accessing nested JavaScript objects and arrays by string path
(44 answers)
Closed 4 years ago.
I'm working with some specific library for forms in javascript. In this library if I have:
{
"id": 1,
"order": 0,
"title": "A title",
},
I can do:
const field = 'order';
form.getState().values[field]
And I'll get the title value.
Now let's say that I have this data:
{
"id": 1,
"order": 0,
"title": "A title",
"Tags": [
{
"id": 1,
"name": "Tag one",
},
{
"id": 2,
"name": "Tag two",
}
]
},
And also I have access to a string with the name of the field for tags, the index of the array, and the field I want to retrieve. But I have all this in a string:
Tags[0].name
If I do:
const field = 'Tags[0].name';
form.getState().values[field]
It will return undefined, because will try to find the key Tags[0].name in the object. So: how can I do to retrieve the name property from a specific index in the Tags array, having Tags[0].name as a string?
A bit nasty but quick and working way to do it is to use eval:
const field = 'Tags[0].name';
let stateValues = form.getState().values;
let tagName = eval(`stateValues.${ field }`)
Other approach might be to split and parse the field path using regexp and then traverse the object property by property.
This question already has answers here:
Object property name as number
(6 answers)
Closed 6 years ago.
Javascript:
var json_obj =
{
"1111": {
"name": Bob,
"id": 1
},
"2222": {
"name": Alice,
"id": 2
}
}
var first_name = json_obj.1111.name; // Gives me a missing ';' before statement error
In above code, json_obj is a part of a large json file used in our project.
I thought of changing json file to make it easy to locate elements but it is a large json file and is used throughout the project, can someone enlighten me as to how to locate elements in these situations?
In such case you can use bracket notation
var json_obj = {
"1111": {
"name": "Bob",
"id": 1
},
"2222": {
"name": "Alice",
"id": 2
}
}
var first_name = json_obj['1111'].name;
document.write(first_name);