I want to insert the data into an array but it shows a warning message that duplicates are not allowed - angularjs

$scope.data = {
"items": [{
id: 1,
name: "Vijay",
email: "vijay#gmail.com",
mobile: "9090909090",
salary: 98690
}, {
id: 2,
name: "ajay",
email: "ajay#gmail.com",
mobile: "9190909090",
salary: 98969
}, {
id: 3,
name: "mahesh",
email: "mahesh#gmail.com",
mobile: "8090909090",
salary: 78555
}, {
id: 4,
name: "ganesh",
email: "ganesh#gmail.com",
mobile: "9099909090",
salary: 48756
}, {
id: 5,
name: "balu",
email: "balu#gmail.com",
mobile: "9880909090",
salary: 98909
}, {
id: 6,
name: "kapil",
email: "kapil#gmail.com",
mobile: "9090907790",
salary: 76595
}]
}
$scope.items = $scope.data.items;
$scope.addItem = function(item) {
$scope.items.filter(function(el) {
if (el.id !== item.id) {
console.log(el.id, "xxxxxxxxxxxx inside if xxxxxxxxxxx", item.id);
$scope.items.push(item);
// console.log($scope.items,item);
$scope.item = {};
} else {
console.log("sdhjkkkkk inside else kkkkkkkkkkkkkkk")
alert("already exist");
}
});

It's because you trying to push element on each iteration of 'filter' loop if id of your item !== item id of array element.
Also, filter is not what you need for this. It returns new array.
You can use 'every' for this. For example:
$scope.addItem = function(item) {
if (checkEquality(item, $scope.items)) {
$scope.items.push(item);
}
};
function checkEquality(element, array) {
return array.every(function(item) {
return item.id !== element.id;
});
}

Related

How to manipulate the object inside the array using javascript?

var arr = [
{ id: 1, name: 'Ahmed Malick', school: 'TEWGS' },
{ id: 2, name: 'Tehmeed Anwar', school: 'DGS' },
{ id: 3, name: 'Azhar Yameen', school: 'DGS' }
]
I want this output:
The student name is his id is and he studies in
Can you please show me what kind of output you expect. Then i will try to solve it.
I'm not sure if this is what you want
var arr = [
{ id: 1, name: "Ahmed Malick", school: "TEWGS" },
{ id: 2, name: "Tehmeed Anwar", school: "DGS" },
{ id: 3, name: "Azhar Yameen", school: "DGS" },
];
arr.map((student) => {
return `Name: ${student.name}, id: ${student.id}, he studies in: ${student.school}`;
}).forEach((output) => {
console.log(output);
});
If you want it in the DOM do this
let html = arr.map((student) => {
return `<p><strong>Name</strong>: ${student.name}, <strong>id</strong>: ${student.id},<strong> he studies in</strong> ${student.school}</p>`;
}).join("")
document.createElement("div").innerHTML = html
Try thatGood luck

How to filter array based on id in angularJS

i have multiple data for one id , i want filter my data like this
$scope.mpArray =[
{ Id: 1, Name: Madhu, Address: Upal },
{ Id: 1, Name: Chandu, Address: Upal },
{ Id: 2, Name: Srinu, Address: Kphb },
{ Id: 2, Name: Vijay, Address: kphb },
{ Id: 3, Name: Ajay, Address: Banglore },
{ Id: 3, Name: Narsi, Address: Banglore },
{ Id: 3, Name: Peter, Address: Banglore },
];
i want to filter my array like this
var FilterArray = [
{ Id: 1,Madhu, Chandu},
{ Id: 2, Srinu, Vijay},
{ Id: 3, Ajay, Narsi, Peter},
];
At first you need to change your FilterArray to
[
{
"Id": 1,
"Name": [
"Madhu",
"Chandu"
]
},
{
"Id": 2,
"Name": [
"Srinu",
"Vijay"
]
},
{
"Id": 3,
"Name": [
"Ajay",
"Narsi",
"Peter"
]
}
]
Notice that name is an array. The FilterArray of your question
var FilterArray = [
{ Id: 1,Madhu, Chandu},
{ Id: 2, Srinu, Vijay},
{ Id: 3, Ajay, Narsi, Peter},
];
Do not contain a valid JSON object inside the array so you need to change the structure to the one where add a new key Name in the JSON object of FilterArray as like the first structure above. Then the below code works great.
$(document).ready(function(){
var myArray =[
{ Id: 1, Name: "Madhu", Address: "Upal" },
{ Id: 1, Name: "Chandu", Address: "Upal" },
{ Id: 2, Name: "Srinu", Address: "Kphb" },
{ Id: 2, Name: "Vijay", Address: "kphb" },
{ Id: 3, Name: "Ajay", Address: "Banglore" },
{ Id: 3, Name: "Narsi", Address: "Banglore" },
{ Id: 3, Name: "Peter", Address: "Banglore" },
];
var FilterArray = [];
var matched;
for(var i=0;i<myArray.length; i++){
matched = false;
var myArrayId = myArray[i].Id;
for(var j=0; j<FilterArray.length; j++){
var FilterArrayId = FilterArray[j].Id;
if(myArrayId === FilterArrayId){
matched = true;
FilterArray[j].Name.push(myArray[i].Name);
// no need to loop further
break;
}
}
if(!matched){
var obj = {
'Id' : myArrayId,
'Name' : [myArray[i].Name],
}
FilterArray.push(obj);
}
}
console.log(FilterArray);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
try this
var mpArray =[
{ Id: 1, Name: 'Madhu', Address: 'Upal' },
{ Id: 1, Name: 'Chandu', Address: 'Upal' },
{ Id: 2, Name: 'Srinu', Address: 'Kphb' },
{ Id: 2, Name: 'Vijay', Address: 'kphb' },
{ Id: 3, Name: 'Ajay', Address: 'Banglore' },
{ Id: 3, Name: 'Narsi', Address: 'Banglore' },
{ Id: 3, Name: 'Peter', Address: 'Banglore' },
];
var filterObject = {};
mpArray.forEach(function (item) {
if (!filterObject[item.Id]) {
filterObject[item.Id] = [];
}
filterObject[item.Id].push(item.Name);
});
console.log(filterObject);
$scope.mpArray =[
{ Id: 1, Name: 'Madhu', Address: 'Upal' },
{ Id: 1, Name: 'Chandu', Address: 'Upal' },
{ Id: 2, Name: 'Srinu', Address: 'Kphb' },
{ Id: 2, Name: 'Vijay', Address: 'kphb' },
{ Id: 3, Name: 'Ajay', Address: 'Banglore' },
{ Id: 3, Name: 'Narsi', Address: 'Banglore' },
{ Id: 3, Name: 'Peter', Address: 'Banglore' },
];
var FilterArray = [];
var FilteredArrayIds=[];
$scope.mpArray.forEach(
function(detailObj) {
if(FilteredArrayIds.indexOf(detailObj.Id)==-1)
return FilteredArrayIds.push(detailObj.Id);
});
for(var i=0; i<FilteredArrayIds.length;i++)
{
var result = $scope.mpArray.filter(function( obj ) {
return obj.Id == FilteredArrayIds[i];
});
var rsltNames = result.map(function(obj){
return obj.Name;
})
var filteredObj ={
id:FilteredArrayIds[i]+',' +rsltNames.join()
}
FilterArray.push(filteredObj);
}
console.log(filteredObj)

Parse object in an array with Node.js

I receive a json code from a database with an object into an array.
I don't find how to parse this code.
This is the json code :
[ { name: 'John1', surname: 'Doe1' },
{ name: 'John2', surname: 'Doe2' },
{ name: 'John3', surname: 'Doe3' },
{ name: 'John4', surname: 'Doe4' } ]
I want to get the name and the surname.
var arr = [ { name: 'John1', surname: 'Doe1' },
{ name: 'John2', surname: 'Doe2' },
{ name: 'John3', surname: 'Doe3' },
{ name: 'John4', surname: 'Doe4' } ]
for(i=0; i<arr.length; i++) {
console.log(arr[i].name);
console.log(arr[i].surname);
}
Store it in a variable. It's an array of JSON.
var foo = [ { name: 'John1', surname: 'Doe1' },
{ name: 'John2', surname: 'Doe2' },
{ name: 'John3', surname: 'Doe3' },
{ name: 'John4', surname: 'Doe4' } ]
foo[0] is { name: 'John1', surname: 'Doe1' }
Now, foo[0].name, foo[0].surname can be used to access the value pairs.
To do this in a better way, use loops
Hope this helps.
So, you need to use a for loop for this and have to iterate through each element.
Here is how you can do this:
First assign your array into a variable named userData.
var userData = [ { name: 'John1', surname: 'Doe1' },
{ name: 'John2', surname: 'Doe2' },
{ name: 'John3', surname: 'Doe3' },
{ name: 'John4', surname: 'Doe4' }]
Now loop over your array userData:
for(i=0; i < userData.length; i++) {
console.log(userData[i].name + ' ' + userData[i].surname);
}
The output will be like this:
John1 Doe1
John2 Doe2
John3 Doe3
John4 Doe4

Angular show list in alphabetical order and also show divider

**please anyone can help me i want to print list in Angularjs like this **
enter image description here
Use Order by
$scope.friends = [
{name: 'John', phone: '555-1212', age: 10},
{name: 'Mary', phone: '555-9876', age: 19},
{name: 'Mike', phone: '555-4321', age: 21},
{name: 'Adam', phone: '555-5678', age: 35},
{name: 'Julie', phone: '555-8765', age: 29}
];
<tr ng-repeat="friend in friends | orderBy:'name'">
read more here
You have to filter each group by the letters you want. Here's a Plunker Using this list:
$scope.myList = [{
id: 11,
name: 'Okra'
}, {
id: 12,
name: 'Musa'
}, {
id: 4,
name: 'Sky'
}, {
id: 13,
name: 'India'
}, {
id: 14,
name: 'Rose'
}, {
id: 15,
name: 'Titanic'
}, {
id: 16,
name: 'Onion'
}, {
id: 6,
name: 'Germany'
}, {
id: 17,
name: 'Beer'
}, {
id: 18,
name: 'Run'
}, {
id: 2,
name: 'Garden'
}, {
id: 19,
name: 'Mountain'
}]
One function to get the alphabets between the two:
function genCharArray(charA, charZ) {
var a = [], i = charA.charCodeAt(0), j = charZ.charCodeAt(0);
for (; i <= j; ++i) {
a.push(String.fromCharCode(i));
}
return a;
};
Then your filter:
app.filter("cfilter", function () {
return function (input, x, y) {
var groups = [];
var letters = genCharArray(x, y);
for (var i = 0; i < input.length; i++) {
for (var x = 0; x < letters.length; x++) {
if (input[i].name.substring(0, 1) == letters[x])
groups.push(input[i]);
}
} return groups;
}
});
And your HTML:
<div ng-repeat="w in myList | cfilter: 'A':'H' | orderBy: 'name'">
<div>{{w.name}}</div>
</div>
create one directive pass an array of letter and range of alphabates you want to disaply.
<dummy-directive data="arrayData" range="A-G"></dummy-directive>
<dummy-directive data="arrayData" range="H-L></dummy-directive>
<dummy-directive data="arrayData" range="M-P"></dummy-directive>
<dummy-directive data="arrayData" range="Q-Z"></dummy-directive>
Now question is that how to implement directive?
we will display sorted data.

How to access method in controller from another in angularjs

How can i access a method in tableController from my menuController. Here is my code.i want to call addRow() method from select() in menu controller. these controllers are in different modules.Please Help me.
my menu controller
var menuApp = angular.module('menuApp', []);
menuApp.controller('menuController', ['tableService', function ($scope, tableService) {
$scope.menuItem = [
{
id: 1,
title: "new",
navigate:"N",
child: [{
id: 11,
title: "new11",
navigate: "N",
child: [{
id: 12,
title: "new12",
navigate: "Y",
url:"/Home/index"
}]
}]
},
{
id: 2,
title: "new",
child: [{
id: 21,
title: "new21",
child: [{
id: 22,
title: "new22"
}]
}]
}
];
$scope.select = function (data) {
if (data.navigate == "Y") {
alert(data.url);
tableService.add();
}
}
}]);
my table controller
tableApp.controller('tableController', function ($scope, $rootScope, $filter, $uibModal) {
$scope.filteredPeople = [];
$scope.currentPage = 1;
$scope.pageSize = 10;
$scope.people = [{ id: "1", name: "joe",disable:true },
{ id: "2", name: "bill", disable: true },
{ id: "3", name: "john", disable: true },
{ id: "1", name: "joe", disable: true },
{ id: "2", name: "bill", disable: true },
{ id: "3", name: "john", disable: true },
{ id: "1", name: "joe", disable: true },
{ id: "2", name: "bill", disable: true },
{ id: "3", name: "john", disable: true },
{ id: "1", name: "joe", disable: true },
{ id: "2", name: "bill", disable: true },
{ id: "3", name: "john", disable: true },
{ id: "1", name: "joe", disable: true },
{ id: "2", name: "bill", disable: true },
{ id: "3", name: "john", disable: true }];
$scope.addRow = function () {
debugger;
$scope.people.unshift({
id: "",
name: "",
disable:false
});
$scope.getPage();
}
$scope.getPage = function () {
var begin = (($scope.currentPage - 1) * $scope.pageSize);
var end = begin + $scope.pageSize;
$scope.filteredPeople = $filter('filter')($scope.people, {
id: $scope.idFilter,
name: $scope.nameFilter
});
$scope.totalItems = $scope.filteredPeople.length;
$scope.filteredPeople = $scope.filteredPeople.slice(begin, end);
};
$scope.getPage();
$scope.pageChanged = function () {
$scope.getPage();
};
$scope.open = function () {
$scope.id = generateUUID();
};
$scope.dblclick = function (index) {
for (var i = 0; i < $scope.filteredPeople.length; i++) {
$scope.filteredPeople[i].disable = true;
}
return index.disable = false;
}
$scope.rowSelect = function (rowdata) {
alert(rowdata.name);
}
$scope.openInput = function (index) {
debugger;
var modalInstance = $uibModal.open({
templateUrl: '/Home/index',
controller: 'testController',
resolve: {
items: function () {
return index;
},
cat: function () {
return 'Account';
}
}
});
}
});
Example of a service shared between controllers & directives
/**
* store and share data between controllers & directives
*/
angular.module('interfaceApp').service('shareData', function () {
this.datas = [];
this.add = function (data) {
this.datas.push(data);
};
//retourne l'élément correspondant à la clé ( utilise la date comme clé )
this.getElement = function (key) {
......
return ;
};
this.getDatas = function () {
return this.datas;
};
});
/* Controller */
var searchModule = angular.module('searchModule', []);
// inject the service in the controller
.controller('searchCtrl', function ($scope, shareData ) {
shareData.add( ... );
console.log( shareData.getDatas() );
});
A service is a singleton, so all controllers using it acces to the same datas ( when they call shareData.getDatas() )

Resources