how to count children in object in javascript? - javascript

I have a json array ..I want to refactor the json object ..but I am not able to make as expected output .I want to search all item or object and insert the children of parent ..I try like that
https://jsfiddle.net/nk6v0y36/1/
var new_Arry = [];
for (var i = 0; i < arry.length; i++) {
var obj = {};
var childeren = [];
obj.con_id = arry[i].con_id;
obj.children = [{
con_to: arry[i].con_to
}];
new_Arry.push(obj)
}
console.log(new_Arry)
Expected output:
[{
"con_id": 11,
"children": [{
"con_to": 12
}]
}, {
"con_id": 12,
"children": [{
"con_to": 13
}, {
"con_to": 14
}]
}, {
"con_id": 13,
"children": []
}, {
"con_id": 14,
"children": [{
"con_to": 15
}]
}, {
"con_id": 15,
"children": [{
"con_to": 16
}]
}, {
"con_id": 16,
"children": []
}]
After answer
https://jsfiddle.net/nk6v0y36/7/
Answer not working..

As thisOneGuy said, you are not checking if a con_id already exists in the new array, so for each element in the old array you are creating a corresponding new one. Your expected output though shows you want to merge these multiple con_id values into a single object.
You are also creating objects with con_to properties in the children arrays even when there is no such property, which again your expected output suggests you do not way.
One way to fix this would be:
var new_Arry = [];
var new_Hash = [];
for (var i = 0; i < arry.length; i++) {
if (new_Hash[arry[i].con_id] === undefined ) {
var obj = {
con_id: arry[i].con_id,
children: []
};
if( arry[i].con_to !== undefined ) {
obj.children.push({ con_to: arry[i].con_to });
}
new_Hash[obj.con_id] = new_Arry.push(obj) - 1;
} else if( arry[i].con_to !== undefined ) {
new_Arry[new_Hash[arry[i].con_id]].children.push({ con_to: arry[i].con_to });
}
}
console.log(new_Arry);
The key here is the new_Hash array, which uses the con_id value as its key, and stores the respective index in the new_Arry as its value.
So for each con_id in arry it first checks the new_Hash to see if there is an entry for it. If not then there is no entry in new_Arry, so it creates a new one.
Firstly the basic object, with con_id and a children array is created. It then checks whether there is a con_to property in arry, and only if there is does it add one as an object to the children array.
This object is then added to new_Arry and new_Hash updated so that an element for con_id references the new new_Arry index. As the .push() method returns extends an array and returns the new array length, the new index will always be this value minus one.
However if an entry for con_id was found in new_Hash then all you need to do it add the con_to value to the existing children array. Whilst presumably if there are multiple con_id entries in arry they must contain a con_to value, it also checks this just to be safe. The index number for the new_Arry as returned by new_Hash[arry[i].cond_id].
Incidentally, the reason for explicitly checking for undefined rather than seeing if value equates to true or false to check for its existence is a con_id or con_to value of 0 would be treated as false and skipped.

Like this -
var new_Arry = [];
for (var i = 0; i < arry.length; i++) {
var obj = {};
var childeren = [];
obj.con_id = arry[i].con_id;
var child = [];
var childrenObj = {};
childrenObj.con_to = arry[i].con_to;
child.push(childrenObj);
obj.children = child;
new_Arry.push(obj)
}
https://jsfiddle.net/nk6v0y36/3/

Here is the correct answer with the correct output : https://jsfiddle.net/nk6v0y36/4/
Basically you need to loop through your array to check if the con_id exists in that array, if it does, just add to children, if it doesnt add a new object.
var new_Arry = [];
for (var i = 0; i < arry.length; i++) {
var obj = {};
var childeren = [];
if (new_Arry.length > 0) {
console.log('start')
console.log(new_Arry.length);
var thisBool = false;
for (var j = 0; j <= new_Arry.length; j++) {
console.log('inside')
if (new_Arry[j]) {
if (new_Arry[j].con_id === arry[i].con_id) {
console.log('has children')
new_Arry[j].children[new_Arry[j].children.length] = {
con_to: arry[i].con_to
}
} else {
thisBool = true;
}
}
}
if(thisBool){
obj.con_id = arry[i].con_id;
obj.children = [{
con_to: arry[i].con_to
}];
new_Arry.push(obj)
}
} else {
console.log('no children')
obj.con_id = arry[i].con_id;
obj.children = [{
con_to: arry[i].con_to
}];
new_Arry.push(obj)
}
}
Check the output in the console log to see it's correct :)

Related

JavaScript - count occurrence of strings

I'm looking to count the occurances of certain strings within JSON - in this instance sensorUUID.
var newDataArray = JSON.stringify(conData);
JSON
[{"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"22489710","timeStamp":1500362037.111941,"uID":"22489710_3_10"},{"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"22489710","timeStamp":1500362036.109874,"uID":"22489710_3_10"}]
I've tried the following code but it is returning an empty object.
var obj = {};
for (var i = 0, j = newDataArray.length; i < j; i++) {
if (obj[newDataArray[i].sensorUUID]) {
obj[newDataArray[i]]++;
}
}
console.log(obj);
The full JSON file will have multiple sensor ID's within it, I am looking to return the number of unique sensor ID.
e.g.
22489710 has 10 occurrences
63846683 has 23 occurrences
etc.
the if condition in for loop is correct but you have to initialize count as 1 for the first time you find a particular sensorUUID.
var newDataArray = [{"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"22489710","timeStamp":1500362037.111941,"uID":"22489710_3_10"},{"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"22489710","timeStamp":1500362036.109874,"uID":"22489710_3_10"}];
var obj = {};
for (var i = 0, j = newDataArray.length; i < j; i++) {
if (obj[newDataArray[i].sensorUUID]) {
obj[newDataArray[i].sensorUUID]++;
}else{
obj[newDataArray[i].sensorUUID] = 1;
}
}
// obj gives you count for each unique sensorUUID.
console.log(obj);
//if you want total count of all sensorUUID you can sum all the values in obj.
var count = Object.values(obj).reduce((a, b) => a + b, 0);
console.log(count);
you can set a variable count and iterate over the array using Array#forEach and check whether the object has the property sensorUUID using Object#hasOwnProperty if yes, increment the count
var data = [{"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"22489710","timeStamp":1500362037.111941,"uID":"22489710_3_10"},{"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"22489710","timeStamp":1500362036.109874,"uID":"22489710_3_10"}];
var count = 0;
data.forEach((x)=>{
if(x.hasOwnProperty('sensorUUID'))
count++;
});
console.log(count);
You can simply iterate through the json array using array.reduce and count the occurances of sensorUUID and store it inside the new object.
var json = [{
"blobJson": "x",
"deviceMfg": 10,
"eventCode": 0,
"sensorClass": 3,
"sensorUUID": "22489710",
"timeStamp": 1500362037.111941,
"uID": "22489710_3_10"
}, {
"blobJson": "x",
"deviceMfg": 10,
"eventCode": 0,
"sensorClass": 3,
"sensorUUID": "22489710",
"timeStamp": 1500362037.111941,
"uID": "22489710_3_10"
}, {
"blobJson": "x",
"deviceMfg": 10,
"eventCode": 0,
"sensorClass": 3,
"sensorUUID": "22489710123",
"timeStamp": 1500362036.109874,
"uID": "22489710_3_10"
}];
let count = json.reduce((newObj, obj) => {
if(newObj[obj.sensorUUID]) {
newObj[obj.sensorUUID] = newObj[obj.sensorUUID]+1;
} else {
newObj[obj.sensorUUID] = 1;
}
return newObj;
}, {});
console.log(count);
https://jsfiddle.net/7jjoches/1/
Using jquery method $.parseJSON you have to convert the JSON string to a JSON object and only then you can work with it.
var conData = '[{"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"22489710","timeStamp":1500362037.111941,"uID":"22489710_3_10"},{"blobJson":"x","deviceMfg":10,"eventCode":0,"sensorClass":3,"sensorUUID":"22489710","timeStamp":1500362036.109874,"uID":"22489710_3_10"}]';
var newDataArray = $.parseJSON(conData);
console.dir(newDataArray);
var obj = {};
for (var i = 0; i< newDataArray.length; i++) {
obj[newDataArray[i].sensorUUID] = obj[newDataArray[i].sensorUUID] ? obj[newDataArray[i].sensorUUID]+1 : 1;
}
console.log(obj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Check for duplicates in an array

I have a function that will check a serialized form data if there are duplicates values in it.
s = $('#multiselectForm').serialize();
var x = [];
var y = [];
x = s.split("&");
for (var i = x.length - 1; i >= 0; i--) {
y.push(x[i].split("="));
};
var c = 0;
var e = 0;
for (var i = y.length - 1; i >= 0; i--) {
if (y[i][1] == y[c][1]) {
e++;
$('.duplicateAlert').show();
} else {
$('.duplicateAlert').hide();
};
c++;
};
Basically, what it does is split the string produced by the serialize() function and push the data into arrays.
The array I'm trying to parse looks like this:
Array [
Array [
0: 'my_field1',
1: 'val1'
],
Array [
0: 'my_field2'
1: 'val2'
],
Array [
0: 'my_field3'
1: 'val1'
]
]
Are there any better ways to do the same task? Maybe even shorter?
Create an empty array to hold the matches
Loop through the array. On each iteration...
Loop through the matches array and check if an item with the same value exists. If it does, set the matched flag.
Check if the matched flag has been set
if so, alert the user
if not add the item to matches.
var array = [
[ 'my_field1', 'val1' ],
[ 'my_field2', 'val2' ],
[ 'my_field3', 'val1' ],
[ 'my_field4', 'val2' ],
[ 'my_field5', 'val3' ]
], matches = [], match = false;
for(var i = 0, j = array.length; i < j; i++) {
match = false;
for(var k = 0, l = matches.length; k < l; k++) {
if(matches[k][1] == array[i][1]) {
match = true;
}
}
if(match) alert('Duplicate!');
else matches.push(array[i]);
}
If you have serialised data in the typical format like:
var data = 'foo=foo&bar=bar%26bar&blah=foo';
then you can check it for duplicates by getting the values between = and & and looking for dupes:
var seen = {};
var hasDupes = (data.match(/=[^&]+/g) || []).some(function(v){
return v in seen || (seen[v] = true) && false;
});
console.log(hasDupes); // true
The idea behind:
data.match(/=[^&]+/g) || []
is that match can return null if no matches are found, so if that happens the expression returns an empty array and the following call to some is called on the empty array (and returns false) rather than null, and hence doesn't throw the error that it would otherwise.
However, I still think it would be more efficient to check the form control values directly before serialising, rather than serialising the form then checking the result.
You can do that with a function like:
function checkDupValues(form) {
var value,
seen = {},
controls = form.elements;
for (var i=0, iLen=controls.length; i<iLen; i++) {
// Might want to check type of control here and ignore buttons, etc.
value = controls[i].value;
// Ignore empty controls?
if (value != '' && value in seen) {
// have a duplicate value that is not ''
alert('have dupes');
} else {
seen[value] = true;
}
}
}
Try this although its not much shorter:
var array = [
[
'my_field1',
'val1'
],
[
'my_field2',
'val2'
],
[
'my_field3',
'val1'
]
]
var originals = [];
var duplicates = [];
for (a in array) {
if (originals.indexOf(array[a][1] == -1)) {
originals.push(array[a][1])
} else {
duplicates.push(array[a])
}
}
alert('duplicates: ' + duplicates.join(', '));

Javascript transform array into grouped object by value

I have an array:
["car1-coupe", "car2-convertible", "car2-hatchback", "car2-estate", "car3-hatchback", "car3-estate"]
The array can have different sets of cars, and I want to turn it into something like this:
[{
car1: ["car1-coupe"]
},{
car2: ["car2-convertible", "car2-hatchback", "car2-estate"]
},{
car3: ["car3-hatchback", "car3-estate"]
}]
How can I do this in JavaScript or Underscore?
So, assuming an array like this:
var a = ["car1-coupe", "car2-convertible", "car2-hatchback", "car2-estate", "car3-hatchback", "car3-estate"];
You can do this:
var b = a.reduce(function(prev, curr){
var car = curr.split('-')[0]; // "get" the current car
prev[car] = prev[car] || []; // Initialize the array for the current car, if necessary.
prev[car].push(curr); // Add the current item to the array.
return prev;
}, {});
This will return the following object:
{
car1: ["car1-coupe"],
car2: ["car2-convertible", "car2-hatchback", "car2-estate"],
car3: ["car3-hatchback", "car3-estate"]
}
var array = ["car1-coupe", "car2-convertible", "car2-hatchback", "car2-estate", "car3-hatchback", "car3-estate"];
var result = {};
for (var i = 0; i < array.length; i++) {
var key = array[i].split('-')[0]; // The car we're interested in
if (result[key]) { // Check if this car has already been initialized
result[key].push(array[i]); //add this model to the list
} else {
result[key] = [array[i]]; // initialize the array with the first value
}
}
console.log(result);
/*will return :
{
car1: ["car1-coupe"],
car2: ["car2-convertible", "car2-hatchback", "car2-estate"],
car3: ["car3-hatchback", "car3-estate"]
}
*/
var myObj = {}, myArr = [];
for( var i = 0; i < arr.length; i+=1) {
var key = arr[i].split("-")[0];
myObj = {};
myObj[key] = [];
for( var j = i; j < arr.length; j+=1 ) {
if( key === arr[j].split("-")[0])
myObj[key].push(arr[j]);
}
myArr.push(myObj);
}
I think this can be done simply with this way. One loop to get the key and another inner loop to get all values of this key.

Trying to loop through arrays containing arrays containing objects, to match data

I'm tryign to write code that will loop through an array "productsArray" and match it against my productPropertyArray to pull matching information.
however productsArray is an array in an array that contains an object with the data. My Question is how can I loop through both arrays and then return the matching data.
Current function:
var pList = productsArray
if (productPropertyArray.length === 0 || productsArray.length === 0) return [];
for (var i = 0; i < pList.length; i++) {
for (var j = 0; j < pList[i].length; j++) {
if (pList[i][j] === productPropertyArray) {
return productPropertyArray;
} else {
continue;
}
}
}
return [];
};
example of pList:
productsArray = [
[{"sku" : "131674"},
{"sku" : "84172"}],
[{"productID" : "1234"}
,{"productID" : "12345"}],
[{"test": 1},{"test": 1}],
[{"test": 1},{"sellAlone": false,"test": 1}],
[{"test": 1}],
[{"sellAlone": false,"test": 1}]
];
example of productPropertyArray: (its an argument thats replaced by the following)
productSKUArray = [
"00544MF24F575",
"131674",
"84172"
];
productPropertyArray is just an argument in the function which is replaced by productSKUArray The setup goes like this: function(productProperty, productPropertyArray, productsArray) {
productProperty is just a string that contains sku or productID
any ideas are appreciated. thanks.
Check this out:
http://jsfiddle.net/v9d7bjms/2/
function find() {
var productsArray = [
[{"sku" : "131674"},
{"sku" : "84172"}],
[{"productID" : "1234"}
,{"productID" : "12345"}],
[{"test": 1},{"test": 1}],
[{"test": 1},{"sellAlone": false,"test": 1}],
[{"test": "00544MF24F575"}],
[{"sellAlone": false,"test": 1}]
],
pList = productsArray,
productSKUArray = [
"00544MF24F575",
"131674",
"84172"
];
// All arrays matching your productsSKUArray
var findings = productsArray.filter(function (productProperty) {
// .some returns true after finding first matching element (and breaks the loop)
return productProperty.some(function (obj) {
var keys = Object.keys(obj);
// We need to get all the "values" from object so we interate over
// the keys and check if any value matches something from productSKUArray
return keys.some(function (key) {
// Check if value exists in productsSKUArray
return productSKUArray.indexOf(obj[key]) > -1;
});
});
});
return findings;
}
console.log(find());
.filter will return all arrays containing objects with values from productSKUArray.
See Array.prototype.filter, Array.prototype.some and Array.prototype.indexOf for method reference.
The inner if needs to refer to pList[i][j].
This will output [{sku: "131674"}, {sku: "84172"}].
var matchingData = [];
for(var productProperties in productsArray){
var pp = productsArray[productProperties];
for(var property in pp) {
var p = pp[property];
for(var propertyName in p){
var propertyValue = p[propertyName];
for(var i in productSKUArray){
if(propertyValue == productSKUArray[i]){
matchingData.push(p);
break;
}
}
}
}
}
but this is just the brute force solution.

Building Array in order of income objects

I'm getting an XML and parsing it, saving it to array, the problems is that I get objects in this order:
temp1.ID = 15
temp1.name = "Dan"
temp1.phone = "32332"
temp2.ID = 12
temp2.name = "Test"
temp2.phone = 53463
temp3.ID = 2
temp3.name = "Tom"
temp3.phone = 12443
.
.
.
.
Object - its an objects that I get inside a loop while parsing XML
What I try is to save them in the same order I started to read them : Array: [temp1,temp2,temp3]
But The result of the next function is : Array: [temp3,temp2,temp1]
the function:
this.mytempect = [];
for (var i = 0; i < xml.length; i++) {
var temp = {};
temp.ID = parseXmlByTag(xml[i], "ID");
temp.name = parseXmlByTag(xml[i], "name");
temp.phone = parseXmlByTag(xml[i], "phone");
if (this.mytempect [temp .ID] == null) {
this.mytempect [temp .ID] = [];
}
this.mytempect [temp .ID].push(obj);
}
Before I save each object I check if I need to create for him a new Key or to add to existing one, in the end I get something like this:
I need to save the order in which I'm getting them so I'll save them in the order I entered them
If I understand your question here's what I think you should be doing. You seem to be confusing objects and arrays: mytempect needs to be an object if you want to store arrays against a key set by the ID.
Following your example, objects with the same key are assigned to the same array (identified by that key in the object) in the order in which they are read.
// create an object, not an array
this.mytempect = {};
for (var i = 0; i < arr.length; i++) {
var temp = {};
temp.ID = arr[i].ID;
temp.name = arr[i].name;
temp.phone = arr[i].phone;
// Don't check for null here because `this.mytempect[temp.ID]` might not exist
if (!this.mytempect[temp.ID]) {
this.mytempect[temp.ID] = [];
}
this.mytempect[temp.ID].push(temp);
}
DEMO
The demo produces an object with one object in an array under key 15, two under 12 and one under 2:
{
"2": [
{
"ID": 2,
"name": "Tom",
"phone": 12443
}
],
"12": [
{
"ID": 12,
"name": "Test",
"phone": 53463
},
{
"ID": 12,
"name": "Test",
"phone": 53462
}
],
"15": [
{
"ID": 15,
"name": "Dan",
"phone": "32332"
}
]
}
Note: you can't order the object in any way.
Perhaps you're looking for something like this
var mytempect = [],
dict = {},
i,
tmp;
for (i = 0; i < xml.length; ++i) {
tmp = {
ID: parseXmlByTag(xml[i], "ID"),
name: parseXmlByTag(xml[i], "name"),
phone: parseXmlByTag(xml[i], "phone")
};
if (!(tmp.ID in dict)) {
mytempect.push(dict[tmp.ID] = []);
}
dict[tmp.ID].push(tmp); // use fact Objects ByRef to add item
}
dict = null; // cleanup
The Array mytempect will now have indices 0, 1, 2, etc containing Arrays of all Objects which have the same ID. With your sample data you will get
mytempect[0][0].ID === 15;
mytempect[1][0].ID === 12;
mytempect[2][0].ID === 2;

Categories