This is my json object adminDocs in angularJS.
[] 0: {rowno: 1, docTypeDesc: "Passport photocopy", …} 1: {rowno: 2, docTypeDesc: "Birth Certificate", …} 2: {rowno: 3, docTypeDesc: "Admission Doc Literature", …} 3: {rowno: 4, docTypeDesc: "Transcript", …} length: 4 __proto__: Array(0)
Checking $scope.adminDocs.length is returning zero.
Try something like this
var count = Object.keys(adminDocs).length;
Try to parse the object and then use:
var obj = JSON.parse(*your JSON*);
var length = Object.keys(obj).length;
My issue is not with $scope.adminDocs.length returning zero. The object's value is not returned untill it is completely loaded. So, it is returning zero.
Related
I have an array of objects on an array called 'houseList = []' - the data is shown below:
0: {houseCode: '1234', street: 'Grove Street'}
1: {houseCode: '5678', street: 'Pike Street'}
2: {houseCode: '9010', street: 'Park Street'}
I am trying to iterate over that data and add to an object a specific index - I want to add the following property to object #1, so that the data on the array of objects would look like this:
0: {houseCode: '1234', street: 'Grove Street'}
1: {houseCode: '5678', street: 'Pike Street', parking: 'True'}
2: {houseCode: '9010', street: 'Park Street'}
So far, I've tried to push on to the array of objects, but no success. My function for doing so is below. I get an error of "ERROR TypeError: this.houseList[i].push is not a function.
for(let i = 0; i < this.houseList.length; i++) {
if (i === 1) {
this.houseList[i].push({parking: this.ParkingIsTrue})
}
}
I have also tried splicing, but that just creates a new object on the array. Here is my code for the splicing function:
for(let i = 0; i < this.houseList.length; i++) {
if (i === 1) {
this.houseList.splice(i, 0, this.ParkingIsTrue))
}
}
The result of the splicing is:
0: {houseCode: '1234', street: 'Grove Street'}
1: {houseCode: '5678', street: 'Pike Street'}
2: {'True'}
3: {houseCode: '9010', street: 'Park Street'}
My question is, how do I actually go about coding the desired data addition?
What you want to do doesn't involve editing an array, only editing the property of that array. Array.prototype.push and Array.prototype.splice are used for adding and removing elements to and from an array, and aren't what you want to use here. Instead, you just want to set the property on the object at the first index of your array, which you can do like this:
this.houseList[1].parking = this.ParkingIsTrue;
Of course, you might need to tweak that slightly depending on how you need to determine which element in the array needs to be edited, and what its value needs to be. But I hope that example is at least a useful guide to the syntax that you will need to use.
The reason why you were getting the error "ERROR TypeError: this.houseList[i].push is not a function" is that this.houseList[i] resolves to an object like {houseCode: '5678', street: 'Pike Street'}. This value is not an array, and doesn't have a push method, so this.houseList[i].push resolves to undefined.
When you then tried to call that like a function, you get an error complaining that it's not a function because, well, it wasn't a function. It was undefined.
You can work with append parking property to object as below:
this.houseList.splice(i, 0, {
...this.houseList[i],
parking: this.ParkingIsTrue,
});
Sample Solution on StackBlitz
References
JavaScript Ellipsis: Three dots ( … ) in JavaScript
I have researched a few questions on StackOverflow but couldn't find a relevant answer.
I have a string of array like this:
"[{"insert":"Test"},{"insert":"\n","attributes":{"header":1}},{"insert":"Asdasfff"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"Asadsf"},{"insert":"\n","attributes":{"list":"bullet"}},{"insert":"Sfsfsft"},{"insert":"\n","attributes":{"header":2}},{"insert":"Make"},{"insert":"\n","attributes":{"list":"unchecked"}},{"insert":"Back"},{"insert":"\n","attributes":{"list":"checked"}},{"insert":"Ban"},{"insert":"\n","attributes":{"list":"unchecked"}},{"insert":"Fg"},{"insert":"\n","attributes":{"list":"checked"}}]"
and I need to convert it to an array as shown below:
I tried JSON.parse() but it didn't work!
It is because of the \n in your JSON. Here an explanation: https://stackoverflow.com/a/5191059/11749096
function jsonEscape(str) {
return str.replace(/\n/g, "\\\\n").replace(/\r/g, "\\\\r").replace(/\t/g, "\\\\t");
}
var data = `[{"insert":"Test"},{"insert":"\n","attributes": 0}]`;
var dataObj = JSON.parse(jsonEscape(data));
console.log(dataObj);
You have not a string of array, the first and the last double quote must be one quote in your case because when you stringify an array it works like this :
const goo = [{"insert":"Test"},{"insert":"\n","attributes":{"header":1}}]
to stringify it :
JSON.stringify(goo)
'[{"insert":"Test"},{"insert":"\n","attributes":{"header":1}}]'
in this case your parse works like :
JSON.parse(foo)
0: {insert: 'Test'}
1: {insert: '\n', attributes: {…}}
length: 2
[[Prototype]]: Array(0)
i struggle my had now sine several days with a way to do conditional filter of an object array with another object array.
lack on capabilities to properly abstract here... maybe you ahve some ideas.
I have a given Object Array A but more complex
var ArrA = [{
number: 1,
name: "A"
}, {
number: 2,
name: "C"
}]
And i want to filer for all results matiching id of Object Array B
var ArrB = [{
id: 1,
categorie: "wine"
}, {
id: 3,
categorie: "beer"
}, {
id: 10,
categorie: "juice"
}]
And in the best case moving this directly also together with an if condition.... but i was not able to handle it ... here is where i am now ... which is not working....
let newArray = ArrA.filter{$0.number == ArrB.... }.
if (newArray.count != 0){
// Do something
}
is there a lean way to compare one attribute of every object in an array with one attribute of another every object in an array ?
Lets break this down: You need all arrA objects that matches arrB ids, so first thing first you need to map your arrB to a list of id (because you dont need the other infos)
let arrBid = Set(arrB.map({ $0.id })) // [1, 3, 10]
As commented below, casting it to Set will give you better results for huge arrays but is not mandatory though
Then you just need to filter your first arrA by only keeping object that id is contained into arrBid :
let arrAFilter = arrA.filter({ arrBid.contains($0.number) })
[(number: 1, name: "A")]
and voila
I am working on react js application and building comment reply structure. API is returning me an array of comments, but it's not in a comment hierarchy.
My API response is like this:
review: {_id: 35,email: "test#gmail.com", review: "Shavon B does an AMAZING job!! I had another fant…e taking care of my home. Shavon is a rock star!"}
comments: [
0: {_id: 36, status: 1, email: "neha#shandil.com", review: "Shavon B does an AMAZING job!! I had another fant…e taking care of my home. Shavon is a rock star!", parent_id: 35, reply_to:35}
1: {_id: 37, status: 1, email: "archana#gmail.com", review: " Thank you for sharing your review of your home cl…e taking care of my home. Shavon is a rock star!", parent_id: 35, reply_to:36}
2: {_id: 39, status: 1, email: "radhika#dummy-url.com", review: "Shavon B does an AMAZING job!! I had another fant…e taking care of my home. Shavon is a rock star!", parent_id: 35, reply_to:37}
3: {_id: 40, status: 1, email: "archi#123.com", review: "good", parent_id: 35, reply_to:36}
4: {_id: 41, status: 1, email: "test#test.com", review: "Testing", parent_id: 35, reply_to:35}
]
here parent_id means these are comments for any blog with id 35, and reply_to means this is a reply for that particular comment _id, like array at index 1 is a reply for comment at 0 index.
Now I am also getting a new reply at the end of the list. Now I want to show all comments in their hierarchy.
Now the problem is I am getting a simple array with all comments and replies, how can I show them in the hierarchy.
Is this possible to push HTML in between, please suggest me a solution, I want to show comments up to two levels.
You will need to convert comments to tree structure and will need to write recursive logic to process comments.
Function for converting flat list to the tree:
function unflatten(arr) {
var tree = [],
mappedArr = {},
arrElem,
mappedElem;
// First map the nodes of the array to an object -> create a hash table.
for(var i = 0, len = arr.length; i < len; i++) {
arrElem = arr[i];
mappedArr[arrElem._id] = arrElem;
mappedArr[arrElem._id]['children'] = [];
}
for (var id in mappedArr) {
if (mappedArr.hasOwnProperty(id)) {
mappedElem = mappedArr[id];
// If the element is not at the root level, add it to its parent array of children.
if (mappedElem.parent_id) {
mappedArr[mappedElem['parent_id']]['children'].push(mappedElem);
}
// If the element is at the root level, add it to first level elements array.
else {
tree.push(mappedElem);
}
}
}
return tree;
}
Here is working POC of Recursive Component and tree data in action:
https://stackblitz.com/edit/react-7jhe22?file=index.js
The POC shows automatically adding a random comment to mimic the behavior of user adding a comment.
This also shows how you can append at the end of comments array and still generate comment view with help of unflatten function. Since this is recursive, you can reply to any comment!!
How can I get to the value from 'age'?
I already have 'record.data.items', but I can't get any further. I want the value of age. Can anyone help me?
`Ext.util.MixedCollection
...
items: Array[3]
0: c
data: Object
age: "4"
id: "1"
name: "sam"
1: c
2: c
length: 3
__proto__: Array[0]
keys: Array[3]
length: 3
...`
Check out this link:
http://docs.sencha.com/ext-js/4-0/#!/api/Ext.util.MixedCollection
You can get at the items in the collection with the getAt() method.
To iterate through the list, loop 0 to getCount()
for (var i = 0 ; i < record.getCount() ; i ++){
console.log(record.getAt(i).data.age);
}