Get the differences between 2 maps flutter - arrays

I have 2 maps and I want to get the missing elements as a list. For example:
var map1= [
{"name":"name1","email":"name1#email.com"},
{"name":"name2","email":"name2#email.com"},
{"name":"name3","email":"name3#email.com"},
];
var map2= [
{"name":"name1","email":"name1#email.com"},
{"name":"name2","email":"name2#email.com"},
];
Output: [{"name":"name3","email":"name3#email.com"}]
I tried this approach:
var removedElements = map2.where((element) =>
!map1
.contains(element['email']))
.toList();
but it doesn't work. Any help would be great.

try with this
void main() {
var map1 = [
{"name": "name1", "email": "name1#email.com"},
{"name": "name2", "email": "name2#email.com"},
{"name": "name3", "email": "name3#email.com"},
];
var map2 = [
{"name": "name1", "email": "name1#email.com"},
{"name": "name2", "email": "name2#email.com"},
];
var removedElements = [];
var k;
for (var i in map2) {
for (var j in map1) {
if (i["email"] != j["email"]) {
k = j;
}
}
removedElements.add(k);
}
print(removedElements.toSet().toList());
}
Another approach
var removedElements =
map2.where((element) {
for(var i in map1){
if(i["email"] == element["email"]){
return false;
} else{
return true;
}
}
}).toList();
print(removedElements);
output
: [{name: name3, email: name3#email.com}]

Related

Convert JSON.STRINGIFY to json array length is not calculating

Hi i have excel upload functionality when i upload the excel that should save in to the mongodb(database). I have taken the excel cell value as json stringify and also i converted that as parse. now i don't how to get that json length value.
here i have attached my code
function to_json(workbook) {
debugger;
console.log(workbook);
var result = {};
workbook.SheetNames.forEach(function(sheetName) {
$scope.Sheetname=sheetName; // here sheet name is excel sheet name
MainSheetName=sheetName;
var roa = X.utils.sheet_to_json(workbook.Sheets[sheetName]);
if(roa.length > 0){
result[sheetName] = roa;
}
});
return result;
}
var HTMLOUT = document.getElementById('htmlout');
function to_html(workbook) {
HTMLOUT.innerHTML = "";
workbook.SheetNames.forEach(function(sheetName) {
var htmlstr = X.write(workbook, {sheet:sheetName, type:'binary', bookType:'html'});
HTMLOUT.innerHTML += htmlstr;
});
}
var tarea = document.getElementById('b64data');
function b64it() {
if(typeof console !== 'undefined') console.log("onload", new Date());
var wb = X.read(tarea.value, {type: 'base64',WTF:wtf_mode});
process_wb(wb);
}
window.b64it = b64it;
var OUT = document.getElementById('out');
var global_wb;
function process_wb(wb) {
global_wb = wb;
var output = "";
var output1 = "";
switch(get_radio_value("format")) {
case "json":
output = JSON.stringify(to_json(wb), 2, 2);
output1=JSON.parse(output);
break;
case "form":
output = to_formulae(wb);
break;
case "html": return to_html(wb);
default:
output = to_csv(wb);
}
if(OUT.innerText === undefined) OUT.textContent = output;
else OUT.innerText = output;
debugger;
$scope.MainInfo=output1; // this return whole data
console.log(output1);
$scope.jsondata=output1.Sheet1[0].length;// this returns undefined. Here sheet name is excel file sheetName it will taken from excel.
console.log($scope.jsondata);
my output is showing like this
{
"Sheet1": [
{
"Name": "xxx",
"Age": "22",
"Salary": "222222"
},
{
"Name": "yyy",
"Age": "23",
"Salary": "232323"
},
{
"Name": "zzz",
"Age": "23",
"Salary": "232323"
}
]
}
now i want length of Sheet1 how can i get that. can any one tell me
If you were to assign your output to obj getting the length of Sheet1 would be as simple as going obj.Sheet1.length
Try this :
var output = {
"Sheet1": [{
"Name": "xxx",
"Age": "22",
"Salary": "222222"
}, {
"Name": "yyy",
"Age": "23",
"Salary": "232323"
}, {
"Name": "zzz",
"Age": "23",
"Salary": "232323"
}]
};
var newArr = $.grep(output1.Sheet1, function( value, index ) {
if(n.Name == "xxx") {
return n;
}
});
console.log(newArr)
You can use angular.fromJson(yourJson).length functionality.
In your case, it should be angular.fromJson($scope.jsondata).length.
As per my understanding from above code, your are getting response into JSON string format so you can't perform JSON operation over it. try to convert json string to json object using JSON.parse method.Below is sample code in node js.
var parseData = JSON.parse(output1);
var parsedata1 = parseData.Sheet1;
for(var namevalue=0;namevalue<parsedata.length;namevalue++){
console.log("Name value: "+parsedata1[namevalue].Name);
}

pushing values from json into array

JSON:
var res =
{
"response": {
"data": {
"profilesearchsnippet": [
[
{
"profileInfo": {
"firstname": "Sundar",
"lastname": "v",
"gender": "male",
"country": "Afghanistan",
"state": "Badakhshan",
"city": "Eshkashem",
"pincode": "",
"plancode": "T001",
"userid": 13
},
"roleInfo": {
"defaultphotoid": 94
}
}
],
[
{
"profileInfo": {
"firstname": "ghg",
"lastname": "vbhvh",
"gender": "male",
"state": "Badakhshan",
"city": "Eshkashem",
"pincode": "454",
"plancode": "T001",
"userid": 22
},
"roleInfo": {
"defaultphotoid": 171
}
}
]
]
}
}
}
In the above json , I need to move roleInfo.defaultphotoid into var image
JS:
$scope.setimage = res.response.data.profilesearchsnippet[0];
for (var i = 0; i++; i<setimage.length; i++){
var image = [];
image .push(setimage[i].roleinfo.defaultphotoid);
}
I assigned one variable named setimage and from there I am trying to push values of all defaultphotoid in another array image to fetch images of all roleinfos, but I am able to fetch only first value.
This is because var image = [] is inside the for loop. It gets re-initialized after every cycle. You need to put it outside the loop
$scope.setimage = res.response.data.profilesearchsnippet[0];
var image = [];
for (var i = 0; i<$scope.setimage.length; i++){
image.push($scope.setimage[i].roleinfo.defaultphotoid);
}
You could use angular.foreach inorder to iterate the elements and set into the variable.
var log = [];
angular.forEach(res.response.data.profilesearchsnippet[0], function(value, key) {
this.push(setimage[i].roleinfo.defaultphotoid);
}, log);
https://docs.angularjs.org/api/ng/function/angular.forEach
try this:
$scope.setimage = res.response.data.profilesearchsnippet;
var images = [];
for (var i = 0; i<$scope.setimage.length; i++){
image.push($scope.setimage[i][0].roleinfo.defaultphotoid);
}
will work.

Angularjs Splice in Nested Array

Hi can somebody help Removing element from nested json array like this
JSON
[{
"id": 1,
"name": "Furniture & Fixture",
"choice": {
"0": {
"req_goods": "table",
"qty": "10"
},
"1": {
"req_goods": "chair",
"qty": "5"
}
}
}, {
"id": 2,
"name": "Miscellaneous Property",
"choice": {
"0": {
"req_goods": "Office Rent",
"qty": "1"
}
}
}]
here how do I remove choice 1 of id 1 .
HTML
<div ng-repeat="cb in capital_budgets">
<div ng-repeat="choice in choices[$index]">
<input ng-model="cb.choice[$index].req_goods">
<input ng-model="cb.choice[$index].qty">
<button ng-hide="$first" ng-click="removeChoice($parent.$index,$index)">-</button>
</div>
<button ng-click="addNewChoice($index)">+</button>
</div>
JS
$scope.capital_budgets = [{"id":1,"name":"Furniture & Fixture"},
{"id":2,"name":"Miscellaneous Property"}];
$scope.choices = [{}];
$scope.choices[0] = [{}];
$scope.choices[1] = [{}];
$scope.choices[2] = [{}];
$scope.choices[3] = [{}];
$scope.choices[4] = [{}];
$scope.addNewChoice = function(id) {
$scope.choices[id].push({});
};
$scope.removeChoice = function(parent_id, id) {
$scope.choices[parent_id].splice(id, 1);
};
The Above removeChoice() remove last element but I want to remove the element that user choose to remove. please help i have been trying from 2 days.
You can make 'choice' of the array type as follows and use the index of the particular choice in the ng-repeat directive to remove the choice from the choices array.
angular
.module('demo', [])
.controller('DefaultController', DefaultController);
function DefaultController() {
var vm = this;
vm.items = [
{
"id": 1,
"name": "Furniture & Fixture",
"choices": [
{
"id": 1,
"req_goods": "table",
"qty": "10"
},
{
"id": 2,
"req_goods": "chair",
"qty": "5"
}]
}, {
"id": 2,
"name": "Miscellaneous Property",
"choices": [
{
"id": 1,
"req_goods": "Office Rent",
"qty": "1"
}]
}];
vm.removeChoice = removeChoice;
vm.addChoice = addChoice;
function removeChoice(itemId, index) {
for (var i = 0; i < vm.items.length; i++) {
if (vm.items[i].id === itemId) {
vm.items[i].choices.splice(index, 1);
break;
}
}
}
function addChoice(index) {
var id = vm.items[index].choices.length + 1;
vm.items[index].choices.push({
id: id,
req_goods: "",
qty: 0
});
}
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="demo">
<div ng-controller="DefaultController as ctrl">
<div ng-repeat="item in ctrl.items">
<h3>{{item.name}}</h3>
<div ng-repeat="choice in item.choices">
<input type="text" ng-model="choice.req_goods" />
<input type="text" ng-model="choice.qty" />
<button type="button" ng-click="ctrl.removeChoice(item.id, $index)">Remove</button>
</div>
<button type="button" ng-click="ctrl.addChoice($index)">Add</button>
</div>
</div>
</div>
You can remove choice "1" of id 1 using the below code snippet.
var json = [
{
"id": 1,
"name": "Furniture & Fixture",
"choice": {
"0": {
"req_goods": "table",
"qty": "10"
},
"1": {
"req_goods": "chair",
"qty": "5"
}
}
}, {
"id": 2,
"name": "Miscellaneous Property",
"choice": {
"0": {
"req_goods": "Office Rent",
"qty": "1"
}
}
}];
function removeChoice(json, parentId, choice) {
for (var i = 0; i < json.length; i++) {
if (json[i].id === parentId) {
delete json[i].choice[choice];
break;
}
}
}
removeChoice(json, 1, "1");
console.log(json);
If you want the the choice also to be of the same type as its parent element i.e. an array you could change your JSON as follows and do as shown in the below code snippet to remove a choice from the JSON
var json = [
{
"id": 1,
"name": "Furniture & Fixture",
"choices": [
{
"id": 1,
"req_goods": "table",
"qty": "10"
},
{
"id": 2,
"req_goods": "chair",
"qty": "5"
}]
}, {
"id": 2,
"name": "Miscellaneous Property",
"choices": [
{
"id": 1,
"req_goods": "Office Rent",
"qty": "1"
}]
}];
function removeChoice(json, parentId, choiceId) {
for (var i = 0; i < json.length; i++) {
if (json[i].id === parentId) {
for (var j = 0; j < json[i].choices.length; j++) {
if (json[i].choices[j].id === choiceId) {
json[i].choices.splice(j, 1);
break;
}
}
break;
}
}
}
removeChoice(json, 1, 1);
console.log(json);
In both of the above methods I've passed the source you want to modify as a parameter to the removeChoice function whereas you can also directly use a variable available within the scope of execution of the removeChoice function and pass only parentId and choiceId as parameters in the below code snippet, you can replace items with the object on your controller's $scope.If you prefer isolation of the code you can pass the items object as a parameter to the removeChoice function as it won't be dependent on the external components directly being used in the method body, I would suggest to have separation of concerns.
var items = [
{
"id": 1,
"name": "Furniture & Fixture",
"choices": [
{
"id": 1,
"req_goods": "table",
"qty": "10"
},
{
"id": 2,
"req_goods": "chair",
"qty": "5"
}]
}, {
"id": 2,
"name": "Miscellaneous Property",
"choices": [
{
"id": 1,
"req_goods": "Office Rent",
"qty": "1"
}]
}];
function removeChoice(parentId, choiceId) {
for (var i = 0; i < items.length; i++) {
if (items[i].id === parentId) {
for (var j = 0; j < items[i].choices.length; j++) {
if (items[i].choices[j].id === choiceId) {
items[i].choices.splice(j, 1);
break;
}
}
break;
}
}
}
removeChoice(1, 1);
console.log(items);
Try This
$scope.removeChoice = function(parent_id,id) {
var TempArr=[];
var parentLength=$scope.choices[parent_id].length;
for(i=0;i<parentLength;i++ ){
if(parentLength[i]!==id){
TempArr.push(parentLength[i]);
}
if(i==parentLength-1){
$scope.choices[parent_id]=[];
$scope.choices[parent_id]=TempArr;
}
}
};

Parse Objects and Arrays in Json data in AngularJs

I am trying to build an array of objects using the data I get from backend service using angularjs. Here is how I get the data
"mylist": [
{
"name": "Test1",
"moreInfo": {
"moreInfoText": "More test",
},
"companyInfo": {
"companyNameInfo": "ABC",
"url": "http://www.google.com",
}
},
{
"name": "Test2",
"moreInfo": {
"moreInfoText": "More test2",
},
"companyInfo": {
"companyNameInfo": "ABC2",
"url": "http://www.yahoo.com",
}
},
]
I want to parse it so I can combine it all in one array of objects like
[{"name": "Test1", "moreInfoText": "More test","companyNameInfo": "ABC", "url": "http://www.google.com"},{ "name": "Test2", "moreInfoText": "More test2","companyNameInfo": "ABC2", "url": ""}]
Try this:
var flatten = function(object) {
var newObj = {};
for (var key in object) {
var item = object[key];
if (typeof item !== 'object') {
newObj[key] = item;
}
else {
var flattened = flatten(item);
for (var k in flattened) {
newObj[k] = flattened[k];
}
}
}
return newObj;
};
var newList = [];
myList.forEach(function(object) {
newList.push(flatten(object);
});
console.log(newList) //this should be what you want

how to get date comparison working?

I fail to see why this date comparison is not working in js. I have linked a json file with items that have dateproperties to the nggrid. This is what the refresh function looks like:
$scope.refresh = function() {
$http.get('data.jon').success(function(data) {
//$scope.myData = data;
//$scope.myData.splice(0,1);
var i=0;
data.forEach(
function(item) {
i++;
console.log(i);
var itemDate = new Date(item.date).valueOf();
var compareDate = new Date('1/1/2025').valueOf();
if (itemDate < compareDate) {
debugger;
console.log(itemDate);
console.log(compareDate);
$scope.myData.push(item);
}
});
});
};
The data for the grid looks like this:
[
{
"name": "Moroni",
"age": 50,
"date":"1/1/2015"
},
{
"name": "Tiancum",
"age": 43,
"date":"1/1/2016"
},
{
"name": "Jacob",
"age": 27,
"date":"1/1/2017"
},
{
"name": "Nephi",
"age": 29,
"date":"1/1/2018"
},
{
"name": "Enos",
"age": 34,
"date":"1/1/2019"
}
]
I would expect to see 5 dates to be displayed in my table because they all satisfy the request?
Here is a plunker:http://plnkr.co/edit/e4ua6k?p=preview
Why don't you do this instead:
var itemDate = new Date(item.date);
var compareDate = new Date('1/1/2025');
if (itemDate < compareDate) {
.....
}
Why not compare timestamp directly instead like below :-
var itemDate = new Date(item.date).getTime();
var compareDate = new Date('1/1/2025').getTime();
if (itemDate < compareDate) {
}

Resources