This question already has answers here:
Find and remove objects in an array based on a key value in JavaScript
(14 answers)
Closed 5 years ago.
I would like to delete an object from a JSON objects array.
Here is the array:
var standardRatingArray = [
{ "Q": "Meal",
"type": "stars"
},
{ "Q": "Drinks",
"type": "stars"
},
{ "Q": "Cleanliness",
"type": "stars"
}
];
For example how can I delete the object whose key is "Q": "Drinks" ?
Preferably of course without iterating the array.
Thanks in advance.
You have to find the index of the item to remove, so you'll always have to iterate the array, at least partially. In ES6, I would do it like this:
const standardRatingArray = [
{ "Q": "Meal",
"type": "stars"
},
{ "Q": "Drinks",
"type": "stars"
},
{ "Q": "Cleanliness",
"type": "stars"
}
];
const index = standardRatingArray.findIndex(x => x.Q === "Drinks");
if (index !== undefined) standardRatingArray.splice(index, 1);
console.log("After removal:", standardRatingArray);
Related
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 months ago.
I have a json in the format:
"json": {
"schema": {
"type": "object",
"title": "My Table",
"properties": {
"table-1659555454697": {
"type": "array",
"items": {
"type": "object",
"properties": {
"inputTest": {
"type": "string"
},
"columnTwo": {
"type": "string"
}
},
"required": [
"inputTest",
"columnTwo"
]
}
}
},
},
I can obtain a reference to the "table-1659555454697" object with:
console.log(this.jsf.schema.properties);
What I'm struggling with is how to obtain a reference from that element's children to the "required" array's length. Assuming I cannot reference "table-1659555454697" by name since its dynamically generated.
I've tried:
console.log(this.jsf.schema.properties[0].items[0].required.length);
But I get undefined error
You probably want Object.keys, Object.values, or Object.entries. They provide an array of, respectively, the object's keys, the object's values, and the object's key-value pairs (as an array of [key, value] arrays).
const propertyNames = Object.keys(json.schema.properties) // ["table-1659555454697"]
const propertyValues = Object.values(json.schema.properties) // [{ type: "array", items: { ... }]
const propertyEntries = Object.entries(json.schema.properties)
// [ ["table-1659555454697", [{ type: "array", items: { ... }] ]
So we could do what you want like this:
const requiredLength = Object.values(json.schema.properties)[0].items.required.length
You can reference table-1659555454697 by doing
Object.keys(this.jsf.schema.properties)[0]
This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 1 year ago.
I currently have an array that has 2 levels. I am attempting to build two new arrays from the initial array but seem to be stuck with the method to use. I have attempted a forEach(), while loop, as well as push to no avail.
My current array structure:
[
{
"attributes": {
"Summary": 10,
"class": "Blue"
}
},
{
"attributes": {
"Summary": 63,
"class":"Red"
}
}
]
I am looking to build two arrays, namely one for summary values, and one for class values.
Are my approaches of a forEach or while loop on the correct path?
If you have an array and want to transform that into another array, the simplest way is to use map (which basically creates a new array containing the results of running each element through a function):
const arr = [
{
"attributes": {
"Summary": 10,
"class": "Blue"
}
},
{
"attributes": {
"Summary": 63,
"class":"Red"
}
}
];
const summaries = arr.map(e => e.attributes.Summary);
const classes = arr.map(e => e.attributes.class);
If you want to accomplish this using forEach, here's one way to do it:
const aryInitial = [
{
"attributes": {
"Summary": 10,
"class": "Blue"
}
},
{
"attributes": {
"Summary": 63,
"class":"Red"
}
}
];
let arySummary = [];
let aryClass = [];
aryInitial.forEach((obj)=>
{
arySummary.push(obj.attributes.Summary);
aryClass.push(obj.attributes.class);
});
console.log("Summary Array:",arySummary);
console.log("Class Array:",aryClass);
This question already has answers here:
Remove Object From Array if the value is empty in name and value pair js
(3 answers)
Closed 2 years ago.
I am trying to remove this empty key value pair object from this JSON array but it I am unable to figure out on how to do it.
I tried this:
var array = {
"title":[
{"lang":"English","value":"yes"},
{"lang":"Spanish","value":"no"},
{"lang":"German","value":""}
]
}
var result = array['title'].filter(function(x){return x.length});
This gives the following output:
{
"title":[
{"lang":"English","value":"yes"},
{"lang":"Spanish","value":"no"},
{"lang":"German"}
]
}
Output expected:
{
"title":[
{"lang":"English","value":"yes"},
{"lang":"Spanish","value":"no"}
]
}
Change your filter to look for an empty string in the value property:
var array = {
"title": [{
"lang": "English",
"value": "yes"
},
{
"lang": "Spanish",
"value": "no"
}, {
"lang": "German",
"value": ""
}
]
}
var result = array['title'].filter(x=>x.value !== "");
console.log(result);
You should do something like this.
array['title'].filter( element => element.value )
You aren't specifying what it's looking for for each element.
Try changing your filter function to check if the value exists and has a length greater than 0:
function(x) { return x.value && x.value.length > 0 }
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 years ago.
I have multiple JSON objects which are have multiple JSON objects.
Now how do i iterate the Objects in deep.
My JSON structure is like below.
{
"phanim": {
"msg1": {
"data": "Hii ra..",
"date": "Sep 9",
"sender": "Phani",
"type": "text"
},
"msg2": {
"data": "How r u?",
"date": "Sep 9",
"sender": "Phani",
"type": "text"
}
}
}
Probably a recursive function - check if the typeof the current iterated property is an object, if so, iterate that!
var iterateObj = function(obj) {
for (var key in obj) {
if (typeof obj[key] === "object") {
iterateObj(obj[key]);
} else {
console.log(obj[key]);
}
}
}
Note: this won't work if one of your properties is an array - you'll need an additional check and logic for that.
This question already has answers here:
How to efficiently count the number of keys/properties of an object in JavaScript
(19 answers)
Closed 6 years ago.
Is there any way to get the length from the following:
{
"11": {
"id": "456",
"uuid": "b596362a-b5bb-4n94-8fd5-1e9fh6fd877b",
"name": "Test",
"description": "Test"
},
"22": {
"id": "739",
"uuid": "c4ccbddf-5177-482f-b5e8-cdd4f699e6b7",
"name": "Test2",
"description": "Test2"
},
"33": {
"id": "737",
"uuid": "m4ccbddv-5177-482f-b5e8-cdd4f699e6b7",
"name": "Test3",
"description": "Test3"
}
}
The length should be 3. I tried to use JSON.stringify(data).length but this gives the length of the whole string.
In addition to above answer, apparently, modern browsers have an Object.keys function. In this case, you could do this:
Object.keys(jsonArray).length;
You can use following function
function getJsonItemLength(item) {
if (typeof item !== undefined && varNotNull(item) && item) {
if (Array.isArray(item)) {
return item.length;
} else if (typeof item === 'object' ) {
return Object.keys(item).length;
} else if (typeof item === 'string' ) {
return item.length
} else {
return 0;
}
}
return 0;
}
using Object.keys you get the list of keys (in an array) so you could:
Object.keys(obj).length
If you're asking about the length of an associative-array-like object, that's your answer: Length of a JavaScript object
Or to save you the click:
Object.keys(myObj).length;