Javascript JSON creation creating an additional object - javascript

I'm trying to create an JSON from another JSON using Javascript.
Here is my code.
var data = [{
"Id": "123",
"Name": "Abc",
"Amount": 110000
},
{
"Id": "567",
"Name": "DEF",
"Amount": 98000
},
{
"Id": "345",
"Name": "XYZ",
"Amount": 145000
}
];
finalArray(data);
function finalArray(data) {
var nArray = [];
var subArray = new Object();
subArray.total = 0;
subArray.records = {};
subArray.size = 0;
data.forEach(item => {
subArray.total += item.Amount;
subArray.records += item;
subArray.size += 1;
});
nArray.push(subArray);
console.log(nArray);
};
Here In the final object, In records I'm getting the below output.
records: "[object Object][object Object][object Object][object Object]"
expected output is
records: "[object Object][object Object][object Object]"
where as in my actual data, I've 3 records in the input.
Please let me know where I'm going wrong.
Thanks

This should work.
function finalArray(data) {
var nArray = [];
var subArray = new Object();
subArray.total = 0;
subArray.records = []; // records must be array
subArray.size = 0;
data.forEach(item => {
subArray.total += item.Amount;
subArray.records.push(item); // need to push each item to records array
subArray.size += 1;
});
nArray.push(subArray);
console.log(nArray);
};

Code has 2 mistakes.
record should be an array not an object
you need use push method to add data to record. Not addition assignment (+=)
var data = [{
"Id": "123",
"Name": "Abc",
"Amount": 110000
},
{
"Id": "567",
"Name": "DEF",
"Amount": 98000
},
{
"Id": "345",
"Name": "XYZ",
"Amount": 145000
}
];
finalArray(data);
function finalArray(data) {
var nArray = [];
var subArray = new Object();
subArray.total = 0;
subArray.records = [];
subArray.size = 0;
data.forEach(item => {
subArray.total += item.Amount;
subArray.records.push(item);
subArray.size += 1;
});
nArray.push(subArray);
console.log(nArray);
};

Change your function to this-
function finalArray(data) {
var nArray = [];
var subArray = new Object();
subArray.total = 0;
subArray.records=[];
subArray.size = 0;
data.forEach(item => {
subArray.total += item.Amount;
subArray.records += item;
subArray.size += 1;
});
nArray.push(subArray);
console.log(nArray);
};

Use an array to hold the original items.
Also, you'll need push to add an Object to an Array;
var data = [{
"Id": "123",
"Name": "Abc",
"Amount": 110000
},
{
"Id": "567",
"Name": "DEF",
"Amount": 98000
},
{
"Id": "345",
"Name": "XYZ",
"Amount": 145000
}
];
finalArray(data);
function finalArray(data) {
// New data
let nArray = {};
nArray.total = 0;
nArray.size = 0;
// Create an array inside nArray to hold original items
nArray.records = [];
data.forEach((item) => {
nArray.total += item.Amount;
nArray.size += 1;
nArray.records.push(item);
});
console.log(nArray);
};

Related

How to filter out objects if two keys are duplicate

const obj =
[
{
"id":"1",
"name":"a",
"email":"abc#gmail.com",
"expiryType":"premium"
},
{
"id":"2",
"name":"b",
"email":"abc#gmail.com",
"expiryType":"gold"
},
{
"id":"3",
"name":"b",
"email":"test#gmail.com",
"expiryType":"premium"
},
]
can somebody please help me how to filter out objects where email is same but i want to keep the one with expiry Type is premium ? How to achieve this using Javascript
Expected output would be
const obj =
[
{
"id":"1",
"name":"a",
"email":"abc#gmail.com",
"expiryType":"premium"
},
{
"id":"3",
"name":"b",
"email":"test#gmail.com",
"expiryType":"premium"
},
]
Assuming you want to keep the latest year's entry, you can keep a Map of the email addresses and years you've seen. See comments:
// The new list
const filtered = [];
// Known emails
const known = new Map();
// Loop through...
for (const entry of obj) {
// Get this email and expiry
const {email, expiryYear} = entry;
// Get the previous info if any
const previous = known.get(email);
if (previous) {
// If the previous one is older than this one,
// replace it with this one
if (previous.expiryYear < expiryYear) {
filtered[previous.index] = entry;
}
} else {
// Add this to the known list and the filtered array
known.set(email, {
index: filtered.length,
expiryYear
});
filtered.push(entry);
}
}
const obj = [
{
"id":"1",
"name":"a",
"email":"abc#gmail.com",
"expiryYear":"2020"
},
{
"id":"2",
"name":"a",
"email":"abc#gmail.com",
"expiryYear":"2019"
},
{
"id":"3",
"name":"b",
"email":"test#gmail.com",
"expiryYear":"2020"
},
];
// The new list
const filtered = [];
// Known emails
const known = new Map();
// Loop through...
for (const entry of obj) {
// Get this email and expiry
const {email, expiryYear} = entry;
// Get the previous info if any
const previous = known.get(email);
if (previous) {
// If the previous one is older than this one,
// replace it with this one
if (previous.expiryYear < expiryYear) {
filtered[previous.index] = entry;
}
} else {
// Add this to the known list and the filtered array
known.set(email, {
index: filtered.length,
expiryYear
});
filtered.push(entry);
}
}
console.log(filtered);
This has the advantage of not constantly re-scanning the new list for known entries.
You can filter out whole object based on unique key you want as below.
const obj =
[
{
"id": "1",
"name": "a",
"email": "abc#gmail.com",
"expiryType": "premium"
},
{
"id": "2",
"name": "b",
"email": "abc#gmail.com",
"expiryType": "gold"
},
{
"id": "3",
"name": "b",
"email": "test#gmail.com",
"expiryType": "premium"
}
]
function arrayUnique(arr, uniqueKey) {
const flagList = []
return arr.filter(function(item) {
if (flagList.findIndex(flagItem => flagItem[uniqueKey] === item[uniqueKey]) === -1) {
flagList.push(item)
return true
}
})
}
Method Calling....
let newObj = arrayUnique(obj,'email')
Output:
newObj = [
{
"id": "1",
"name": "a",
"email": "abc#gmail.com",
"expiryType": "premium"
},
{
"id": "3",
"name": "b",
"email": "test#gmail.com",
"expiryType": "premium"
}
]
Hope this helps.
You can do it simply with 2 loops. Maybe not the fastes but the simplest:
function deleteDouble(array, objectKey) {
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array.length; j++) {
if (i == j) {
continue;
}
if (array[i][objectKey] == array[j][objectKey]) {
array.splice(i, 1);
i = 0;
j = 0;
break;
}
}
}
return array;
}
deleteDouble(obj, "email");

Display the difference between two JS objects

I'm trying to display the name of property where their values are difference.
Then, I'm trying to display the property which is not present on the other variable
myObj1 = {
"name": "John",
"age": "45",
"car": 20,
"test": 30,
"example": 20
};
myObj2 = {
"name": "John",
"age": "30",
"car": 10,
"example": 10
};
// or
//myObj1 = { "name":"John", "age":"45", "car":20 };
//myObj2 = { "name":"John", "age":"30", "car":10,"test":30 };
// find keys
keyObj1 = Object.keys(myObj1);
keyObj2 = Object.keys(myObj2);
// find values
valueObj1 = Object.values(myObj1);
valueObj2 = Object.values(myObj2);
// find max length to iterate
if (keyObj1.length > keyObj2.length) {
var biggestKey = keyObj1.length;
} else {
var biggestKey = keyObj2.length;
}
// now compare their keys and values
for (var i = 0; i < biggestKey; i++) {
//console.log(biggestKey)
if (keyObj1[i] == keyObj2[i] && valueObj1[i] !== valueObj2[i]) {
console.log('property:' + keyObj1[i]);
console.log('first:' + valueObj1[i]);
console.log('second:' + valueObj2[i]);
}
}
Actual result:
property: age, 45,30
property: car,20,10
property: test,30,10
property: example,20,undefined
Expected result:
property: age, 45,30
property: car,20,10
property: test,30,undefined
property: example,20,10
Problems
biggestKey is a length instead of array
compare object by key in biggestKey
myObj1 = {
"name": "John",
"age": "45",
"car": 20,
"test": 30,
"example": 20
};
myObj2 = {
"name": "John",
"age": "30",
"car": 10,
"example": 10
};
// find keys
keyObj1 = Object.keys(myObj1);
keyObj2 = Object.keys(myObj2);
// find max length to iterate
if (keyObj1.length > keyObj2.length) {
var biggestKey = keyObj1;
} else {
var biggestKey = keyObj2;
}
// now compare their keys and values
for (var i = 0; i < biggestKey.length; i++) {
//console.log(biggestKey)
var key = biggestKey[i];
if (myObj1[key] != myObj2[key]) {
console.log('project:', key, myObj1[key], myObj2[key]);
}
}
Hope it will help you
function getDifference(compareObject, baseObject) {
function difference(compareObject, baseObject) {
return _.transform(compareObject, function(results, value, key) {
if (!_.isEqual(value, baseObject[key])) {
results[key] = (_.isObject(value) && _.isObject(baseObject[key])) ? changes(value, baseObject[key]) : value;
}
});
}
return difference(compareObject, baseObject);
}
The trick is to iterate through both objects and to ignore the properties that exist in both objects in the second loop.
var myObj1 = {
"name": "John",
"age": "45",
"car": 20,
"test": 30,
"example": 20
};
var myObj2 = {
"name": "John",
"age": "30",
"car": 10,
"example": 10
};
for (var key in myObj1) {
if (myObj1[key] !== myObj2[key]) console.log("property:", key, myObj1[key], myObj2[key]);
}
for (key in myObj2) {
if (!(key in myObj2)) { // ignore if key exists in myObj1 (if it exists, then it got checked in the previous loop)
if (myObj1[key] !== myObj2[key]) console.log("property:", key, myObj1[key], myObj2[key]);
}
}

How to get objects with same key values with comma separated

I have an array of objects , each object have key and value .I want if object have same keys then their values shoud be comma sepated of all the values of same key.
my html code:
<p ng-repeat="item in allOptions" class="item" id="{{item.id}}">
{{item.id}} <input type="checkbox" ng-change="sync(bool, item)" ng-model="bool" > {{item}} Selected: {{bool}}
</p>
and my controller code is :
$scope.allOptions = [
{
"id": "1",
"data": "one",
},
{
"id": "1",
"data": "two",
},
{
"id": "2",
"data": "three",
},
];
$scope.data = [
];
$scope.sync = function(bool, item){
if(bool){
// add item
$scope.data.push(item);
} else {
// remove item
for(var i=0 ; i < $scope.data.length; i++) {
if($scope.data[i] == item.id){
$scope.data.splice(i,1);
}
}
}
};
In data array i have objects ,if we select same key of objects (same id value )then i want
{
"id": "1",
"data": "one","two",
}
var myData = [{
"id": "1",
"data": "one",
},{
"id": "1",
"data": "two",
},{
"id": "2",
"data": "three",
}];
var output = [];
//Iterating each element of the myData
myData.forEach(o => {
//Checking the duplicate value and updating the data field
let temp = output.find(x => {
if (x && x.id === o.id) {
x.data += ", " + o.data;
return true;
}
});
if(!temp)
output.push(o);
});
console.log(output);
I think, easiest way to make it would be like:
z = [
{
"id": "1",
"data": "one",
},
{
"id": "1",
"data": "two",
},
{
"id": "2",
"data": "three",
},
];
And immediate code:
var result = {};
var groupedO = {};
for(a in z){
var id = z[a].id;
var data = z[a].data;
if(groupedO[id] && groupedO[id].data){
groupedO[id].data = groupedO[id].data + ',' + data;
} else {
groupedO[id] = {data:data};
}
}
for(ind in groupedO) {
var el = groupedO[ind];
if(el.data.split(',').length > 1) { // here we take only last those, where many datas grouped in
result.id = ind;
result.data = el.data;
}
}
After this, result will look like:
{ id: "1", data: "one,two" }
If you use jQuery, then you can use $.extend() function in this code if you don't want to put reference to the object item in array hash. This means that if you change the object item in array hash, then object item in array myData change too. To avoid this, use $.extend() function.
var myData = [
{
"id": "1",
"data": "one",
},
{
"id": "1",
"data": "two",
},
{
"id": "2",
"data": "three",
},
]; // this will be your input data
function filterData(collection) {
var hash = {};
var result = [];
collection.forEach(function (item) {
if (hash[item.id]) {
hash[item.id].data += ', ' + item.data;
}
else {
hash[item.id] = $.extend({}, item);
}
});
for (var i in hash) {
result.push(hash[i]);
}
return result;
}
var filteredData = filterData(myData); //your filtered data
You can do this in following manner:
var myData = [{
"id": "1",
"data": "one",
},
{
"id": "1",
"data": "two",
},
{
"id": "2",
"data": "three",
},
]; // this will be your input data
function filterData(collection) {
var hash = {};
var result = [];
collection.forEach(function(item) {
if (hash[item.id]) {
hash[item.id].data += ', ' + item.data;
} else {
hash[item.id] = item;
}
});
for (var i in hash) {
result.push(hash[i]);
}
return result;
}
console.log(
filterData(myData) //your filtered data
)

Compare two objects in jQuery and get the difference [duplicate]

This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 7 years ago.
Using jQuery I would like to compare 2 objects:
sourceArray:
var origArray = [{
"Name": "Single",
"URL": "xxx",
"ID": 123
},
{
"Name": "Double",
"URL": "yyy",
"ID": 345
},
{
"Name": "Family",
"URL": "zzz",
"ID": 567
}];
destination array
var destArray = [{
"Name": "Single",
"URL": "xxx",
"ID": 123
},
{
"Name": "Double",
"URL": "yyy",
"ID": 888
},
{
"Name": "Family",
"URL": "zzz",
"ID": 567
}];
What I would like to do, is compare the target object with the source object based on the ID and find the mis-matched entries with a description on the resultant object. So the result will look like this:
var resultArray = [{
"Name": "Double",
"URL": "yyy",
"ID": 888,
"desc": "missing in source"
},
{
"Name": "Double",
"URL": "yyy",
"ID": 345,
"desc": "missing in destination"
}];
Any quick help is really appreciated.
This isn't a good use of jQuery, but here is some vanilla javascript that does what you want.
function objDiff(array1, array2) {
var resultArray = []
array2.forEach(function(destObj) {
var check = array1.some(function(origObj) {
if(origObj.ID == destObj.ID) return true
})
if(!check) {
destObj.desc = 'missing in source'
resultArray.push(destObj)
}
})
array1.forEach(function(origObj) {
var check = array2.some(function(destObj) {
if(origObj.ID == destObj.ID) return true
})
if(!check) {
origObj.desc = 'missing in destination'
resultArray.push(origObj)
}
})
return resultArray
}
https://jsfiddle.net/9gaxsLbz/1/
If you are wanting to dedupe your array, this will work:
var merged = origArray.concat(destArray);
var unique = merged.filter(function(item) {
return ~this.indexOf(item.ID) ? false : this.push(item.ID);
}, []);
Fiddle: https://jsfiddle.net/Ljzor9c6/
If you are only wanting items that were duped, you can easily invert the condition:
var merged = origArray.concat(destArray);
var dupes = merged.filter(function(item) {
return ~this.indexOf(item.ID) ? true : !this.push(item.ID);
}, []);
You can loop through the items in the first array and put the ID's in a map, then loop through the items in the second array and remove the matching ID's and add the missing.
Then just loop through the map to create the objects in the resulting array:
var origArray = [{
"Name": "Single",
"URL": "xxx",
"ID": 123
},
{
"Name": "Double",
"URL": "yyy",
"ID": 345
},
{
"Name": "Family",
"URL": "zzz",
"ID": 567
}];
var destArray = [{
"Name": "Single",
"URL": "xxx",
"ID": 123
},
{
"Name": "Double",
"URL": "yyy",
"ID": 888
},
{
"Name": "Family",
"URL": "zzz",
"ID": 567
}];
var map = {};
for (var i = 0; i < origArray.length; i++) {
map[origArray[i].ID] = 'source';
}
for (var i = 0; i < destArray.length; i++) {
var id = destArray[i].ID;
if (id in map) {
delete map[id];
} else {
map[id] = 'destination';
}
}
var resultArray = [];
for (key in map) {
var arr = map[key] == 'source' ? origArray : destArray;
for (var i = 0; arr[i].ID != key; i++) ;
resultArray.push({
Name: arr[i].Name,
URL: arr[i].URL,
ID: arr[i].ID,
desc: 'missing in ' + map[key]
});
}
// show result in StackOverflow snippet
document.write(JSON.stringify(resultArray));
var result = [];
for(var i = 0; i < oa.length; i++) {
var idx = mIndexOf(oa[i].ID);
if(idx > -1) {
oa.splice(i, 1);
da.splice(idx, 1);
}
}
for(var i = 0; i < oa.length; i++) {
var ln = result.length;
result[ln] = oa[i];
result[ln].desc = "missing in destination";
}
for(var i = 0; i < da.length; i++) {
var ln = result.length;
result[ln] = da[i];
result[ln].desc = "missing in origin";
}
function mIndexOf(id) {
for(var i = 0; i < oa.length; i++)
if(oa[i].ID == id)
return i;
return -1;
}
console.log(result);
0: Object
ID: 345
Name: "Double"
URL: "yyy"
desc: "missing in destination"
1: Object
ID: 888
Name: "Double"
URL: "yyy"
desc: "missing in origin"
jsfiddle DEMO
For things like this, you should use lodash. With lodash you can just do this:
var resultArray = _.defaults(destArray, origArray);

How apply ko.utils.arrayGetDistinctValues on two dimensional arrays

I have an ko.observableArray with two dimensions, i.e. "id" and "name".
Id will always be distinct, but name can hold the same value.
self.myArray = ko.observableArray( [
{ "id": "1", "name": "Barbara" }, 
{ "id": "2", "name": "Edwin" }, 
{ "id": "3", "name": "Barbara" } 
] );
However I want to filter the array so that my result holds only the first occurrence of "name".
The result I want is:
self. myFilteredArray = [
{ "id": "1", "name": "Barbara" }, 
{ "id": "2", "name": "Edwin" } 
];
I have been trying to figure this out using ko.utils.arrayGetDistinctValues(), but how can I utilize it on solely one chosen dimension?
You could use a computed for this:
viewModel.myFilteredArray = ko.computed(function() {
var rv = [], names = [];
for(var i=0; i<this.myArray().length; i++) {
if(names.indexOf(this.myArray()[i].name) === -1) {
rv.push(this.myArray()[i]);
names.push(this.myArray()[i].name);
}
}
return rv;
}, viewModel);
I can suggest a function, that loops through the array, builds map of property values for each item and checks, whether the current value was already added. Something like this:
function filterByFirstOccurance(arr, prop) {
var propValuesHash = {},
result = [];
for (var i = 0, l = arr.length; i < l; i++) {
var item = arr[i],
propValue = item[prop];
if (item.hasOwnProperty(prop) && !propValuesHash[propValue]) {
propValuesHash[propValue] = true;
result.push(item);
}
}
return result;
};
Fiddle

Categories

Resources