If I have an array of objects like this:
"localValues" : [
{
"localValId" : "e3rQACssGkfp9zsue",
"localProductCode" : "271102502",
"localMembersPrice" : 7814.090000000001,
"localProductDescription" : "11R225 146/143L H DUN SP384 FM TL",
"fetPr" : "29.39",
"invPrice" : "353.85"
},
{
"localValId" : "NxtmZngRpGY56grkW",
"localProductCode" : "290132910",
"localMembersPrice" : "300",
"localProductDescription" : "215/70R16 99S DUN GRNDTRK ST20 BSWTL",
"fetPr" : "",
"invPrice" : "136.72"
},
{
"localValId" : "WXLiCMJMixndtQtqZ",
"localProductCode" : "271102502",
"localMembersPrice" : "444",
"localProductDescription" : "11R225 146/143L H DUN SP384 FM TL",
"fetPr" : "29.39",
"invPrice" : "353.85"
}];
Is there a way I can check if a new localProductCode already exists in the localValues array?
Thank you.
you can try:
function isExisted(localValues, localProductCode) {
for (var i = 0; i < localValues.length; ++i) {
if (localValues[i].localProductCode == localProductCode) {
return true;
}
}
return false;
}
This is a way to find the index of coincidence (like indexOf)
Array.prototype.indexOfObj = function(key, value){
for(var i = 0; i < this.length;)
if(this[i++][key] === value) return --i
return -1
}
Items.localValues.indexOfObj("localValId", "NxtmZngRpGY56grkW"); //1
Items.localValues.indexOfObj("localValId", "WXLiCMJMixndtQtqZ"); //2
Demo
Related
I would like to filter an array by unique category in controller and then use the filtered array with ng-repeat in html page. My array is:
$scope.tabs = [
{OBJECTID:1, TYPES:8, Category:"Pharmacies",Name_EN:"antonis" , text : "Home"},
{OBJECTID:2, TYPES:8, Category:"Opticians",Name_EN:"antonis" , text : "Games"},
{OBJECTID:3, TYPES:8, Category:"Doctors", Name_EN:"antonis" , text : "Mail"},
{OBJECTID:4, TYPES:8, Category:"Clinics", Name_EN:"antonis" , text : "Car"},
{OBJECTID:5, TYPES:8, Category:"Clinics", Name_EN:"antonis" , text : "Profile"},
{OBJECTID:6, TYPES:8, Category:"Clinics", Name_EN:"antonis" , text : "Favourites"},
{OBJECTID:7, TYPES:8, Category:"Pharmacies",Name_EN:"antonis" , text : "Chats"},
{OBJECTID:8, TYPES:4, Category:"Sights",Name_EN:"antonis" , text : "Settings"},
{OBJECTID:9, TYPES:4, Category:"Meuseums",Name_EN:"antonis" ,text : "Home"},
{OBJECTID:10, TYPES:4, Category:"Meuseums",Name_EN:"antonis1" , text : "Home"}
];
the filtered array will be like this:
$scope.FilteredArray = [
{Category:"Pharmacies"},
{Category:"Opticians"},
{Category:"Doctors"},
{Category:"Clinics"},
{Category:"Sights"},
{Category:"Meuseums"},
];
Thanks in advance.
Use unique filter inside controller and apply ng-repeat on filtered array
//Filtered array
$scope.filteredArray = $filter('unique')($scope.tabs,'Category');
<ul>
<li ng-repeat="tab in filteredArray">{{tab.Category}}</li>
</ul>
Filter
angular.module('myapp').filter('unique', function () {
return function (items, filterOn) {
if (filterOn === false) {
return items;
}
if ((filterOn || angular.isUndefined(filterOn)) && angular.isArray(items)) {
var hashCheck = {}, newItems = [];
var extractValueToCompare = function (item) {
if (angular.isObject(item) && angular.isString(filterOn)) {
return item[filterOn];
} else {
return item;
}
};
angular.forEach(items, function (item) {
var valueToCheck, isDuplicate = false;
for (var i = 0; i < newItems.length; i++) {
if (angular.equals(extractValueToCompare(newItems[i]), extractValueToCompare(item))) {
isDuplicate = true;
break;
}
}
if (!isDuplicate) {
newItems.push(item);
}
});
items = newItems;
}
return items;
};
});
FULL EXAMPLE
You can also do it without angular filter. If you have used lodash module in your angular application then you can simply do it with below lodash function. It will return uniq records by whatever key you want.
_.uniqBy([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }, { 'x': 2 }]
You can do this too,
$scope.unique = {};
$scope.distinct = [];
for (var i in $scope.tabs) {
if (typeof($scope.unique[$scope.tabs[i].Category]) == "undefined") {
$scope.distinct.push($scope.tabs[i].Category);
}
$scope.unique[$scope.tabs[i].Category] = 0;
}
DEMO
That code could be used to create a method this will give you an array of strings also you can modify to create object array -
var flags = [], $scope.FilteredArray= [], l = $scope.tabs.length, i;
for( i=0; i<l; i++) {
if( flags[$scope.tabs[i].Category]) continue;
flags[$scope.tabs[i].Category] = true;
$scope.FilteredArray.push($scope.tabs[i].Category);
}
Use the default unique filter:
<select ng-model="tab" ng-repeat="uni in tabs | unique:'Category'">
</select>
I've been struggling with that problem for a few days. I have to transform an object into another object with a different structure.
from this:
{
"a1" : "key",
"a2" : {
"b1" : "key",
"b2" : {
"c1" : "key"
}
}
}
to this:
{
"key" : {
"a1" : null,
"a2" : [
{
"b1" : null,
},
{
"b2" : [
"c1" : null
]
}
]
}
}
For better understanding, a1, a2, b1, b3 etc. would represent css selectors. null will be the value applied to the node. But for now, this shouldn't matter.
My current (non working) function looks like this:
var json = {};
var obj = {};
function build(objA){
for(var a in objA){
var key = objA[a];
if(key instanceof Object){
obj = {}; obj[a] = key;
build(key);
} else {
json[key] = json[key] || {};
if(obj == undefined){
json[key][a] = null;
} else {
var p = firstKeyOf(obj);
var key = obj[p][a];
json[key][p] = json[key][p] || [];
var selector = {};
selector[a] = null;
json[key][p].push(selector);
}
}
}
}
which produces:
{
"key" : {
"a1" : null,
"a2" : [
{
"b1" : null
}
],
"b2" : [
{
"c1" : null
}
]
}
}
What did i miss? I appreciate any help, thanks!
Try this
var Converter = (function() {
var pic;
function _rec(a, o) {
for(var k in o){
var s = o[k];
var arr = a.slice();
arr.push(k);
if (s instanceof Object) { _rec(arr, s); }
else {
if ("string" == typeof s) { arr.unshift(s); _attach(pic, arr); }
else console.log('Invalid key type');
}
}
}
function _attach(p, m) {
var k = m.shift();
if ( ! p.hasOwnProperty(k)) p[k] = (m.length == 0) ? null : {};
if (m.length > 0) p[k] = _attach(p[k], m);
return p;
}
function _reverse(obj) {
if ( ! (obj instanceof Object)) console.log('First parameter need to be an Object');
else { pic = {}; _rec([], obj); return pic; }
}
return {
reverse: _reverse,
attach: _attach
}
})();
Trying to split a javascript object in to a hash array.. I have to split the contents inside the array based on the occurrence of symbol"|"
my input array looks like
{
"testFieldNames": ["testNumber", "testName", "testDate1", "testDate2"]
},
"data": [
"4|Sam|2012-02-10T00:00Z",
"0|Wallace|1970-01-01T00:00Z|2012-02-10T00:00Z"
]
};
and the expected output is [{"testNumber" : "4", "testName" : "Sam", "testDate1" : "2012-02-10T00:00Z", "testDate2" : "0"},{"testNumber" : "0", "testName" : "Wallace", "testDate1" : "1970-01-01T00:00Z", "testDate2" : "2012-02-10T00:00Z"}]
This is what I've tried.. but it is not complete.
http://jsfiddle.net/Dwfg6/1/
var header = responseData.header.testFieldNames,
length = header.length,
result;
result = responseData.data.map(function(el) {
var ret = {}, data = el.split('|'), i;
for (i=0; i < length; i++) {
ret[header[i]] = data[i];
}
return ret;
});
console.log(result);
The demo. (Note: you may use jQuery.map methods instead for old browsers.)
You were close...
http://jsfiddle.net/Dwfg6/4/
var responseData = {
"header": {
"testFieldNames": ["testNumber", "testName", "testDate1", "testDate2"]
},
"data": [
"4|Sam|2012-02-10T00:00Z|2012-02-10T00:00Z",
"0|Wallace|1970-01-01T00:00Z|2012-02-10T00:00Z"
]
};
function buildData(fields, data) {
var output = [];
if (fields && fields.length && data && data.length) {
for (var i = 0; i < data.length; i++) {
var row = data[i].split("|");
output[i] = {};
while (row.length) {
output[i][fields[4 - row.length]] = row.shift();
}
}
}
return output;
}
console.log(buildData(responseData.header.testFieldNames, responseData.data));
fiddle : http://jsfiddle.net/FjJse/1/
My answer:
fiddle
function mapData (data) {
'use strict';
var result=[];
var i, j;
var values = [];
var resultObj;
for(i=0; i < data.testFieldValues.length; i += 1) {
values = data.testFieldValues[i].split("|");
resultObj = {};
for(j = 0; j < data.testFieldNames.length; j += 1) {
resultObj[data.testFieldNames[j]] = values[j];
}
result.push(resultObj);
}
return result;
}
//$(document).ready(function() {
// 'use strict';
var data = {testFieldNames: ["testNumber", "testName", "testDate1", "testDate2"],
testFieldValues: [
"4|Sam|2012-02-10T00:00Z|2012-02-10T00:00Z",
"0|Wallace|1970-01-01T00:00Z|2012-02-10T00:00Z"
]
};
console.log(mapData(data));
//});
/*Expected Output [{"testNumber" : "4", "testName" : "Sam", "testDate1" : "2012-02-10T00:00Z", "testDate2" : "2012-02-10T00:00Z"},{"testNumber" : "0", "testName" : "Wallace", "testDate1" : "1970-01-01T00:00Z", "testDate2" : "2012-02-10T00:00Z"}]*/
Hit F12 in Chrome to see console, or open FireBug in FireFox or LadyBug in Opera, etc.
I am converting JavaScript object to array but in result I am getting one null
here my code
<div id="a"></div>
Object.prototype.toArray = function(){
var arr = [];
for (var i in this) {
arr.push(this[i]);
}
return arr;
}
var providers = {
facebooklike: "Facebook Like",
facebookrecommend : "Facebook Recommend",
facebooksend : "Facebook Send",
twittertweet : "Twitter Tweet",
linkedinshare : "LinkedIn Share",
linkedinrecommend : "LinkedIn Recommend",
googleplusplusone : "Google+ +1",
googleplusshare : "Google+ Share"
};
var a = document.getElementById("a");
a.innerHTML = JSON.stringify(providers.toArray());
My result is
["Facebook Like","Facebook Recommend","Facebook Send","Twitter
Tweet","LinkedIn Share","LinkedIn Recommend","Google+ +1","Google+
Share",null]
Here is fiddle example
Add check for own properties (not inherited from the Object proptotype):
Object.prototype.toArray = function(){
var arr = [];
for (var i in this) {
if(this.hasOwnProperty(i))
arr.push(this[i]);
}
return arr;
}
Add hasOwnProperty check.
for (var i in this) {
if (this.hasOwnProperty(i)) {
arr.push(this[i]);
}
}
I am trying to aggregate the names of all the users in on a blog who have replied to each other. I have records as follows:
{
"_id" : ObjectId("4ee9ada4edfb941f3400ba63"),
"thread" : "Millenium - Niels Arden Oplev",
"author" : "kilny17",
"parent_count" : 0,
"parents" : [ ],
"child_count" : 2,
"date" : ISODate("2010-04-20T21:14:00Z"),
"message" : "I don't think so...",
"children" : [
{
"date" : ISODate("2010-04-20T21:21:00Z"),
"author" : "Kissoon"
},
{
"date" : ISODate("2010-04-20T21:49:00Z"),
"author" : "Twain"
}
]
}
I am trying to return, for each author, a MapReduced object such as:
{ "_id" : "kilny17",
"value" : {
"author" : "kilny17",
"connections" : {
"Kissoon" : 1,
"Twain" : 1 }
}
}
This code works for each record that has a children element with just 1 child, but not for more:
function mapf()
{
var count = this['child_count'];
if (count > 0){
var m_author = this.author;
this['children'].forEach( function(c){
var connect = {'name':c['author'], 'appears':1};
emit(m_author, {'author':m_author, 'connections':connect});
});
};
}
function reducef(key, values)
{
var connects = new Object();
var r = {'author':key, 'connections':connects, 'weight':0};
values.forEach(function(v)
{
c_name = v['connections'].name;
if (c_name == null)
c_name = 'Null_name';
if (r['connections'][c_name] != null)
r['connections'][c_name] += v['connections']['appears'];
else
r['connections'][c_name] = v['connections']['appears'];
});
return r;
}
For any record (such as the example given) with more than 1 child, the author names are not found and I get a reduced record like so (N.B. There was another post by kilny with child DarkKnight3657):
{ "_id" : "kilny17", "value" : { "author" : "kilny17", "connections" : { "DarkKnight3657" : 1, "Null_name" : null } } }
Anyone have any ideas as to why the author name is not being read from the Object?
Thanks
I think the problem is that you are not defining connections as an array in the mapper- you are defining it as an element. Off the top of my head, it seems like it should read:
var connect = [{'name':c['author'], 'appears':1}];
emit(m_author, {'author':m_author, 'connections':connect});
As Chris has suggested, the solution I used was to change the object into an array:
function mapf()
{
if (this['child_count'] > 0){
var m_author = this.author;
if ( m_author == '')
m_author = 'Unknown_author';
var connect = [];
var weight = 0;
for ( c in this['children'] ){
c_name = this['children'][c]['author'];
found = false;
for (i in connect){
if (connect[i]['name'] == c_name){
connect[i]['appears'] += 1;
found = true;
}
}
if (found == false){
var con = {'name':c_name,'appears':1};
connect.push(con);
}
weight += 1;
};
emit(m_author, {'author':m_author, 'connections':connect, 'weight':weight});
};
}
function reducef(key, values)
{
var r = {'author':key, 'connections':[], 'weight':0};
values.forEach(function(v)
{
for ( c in v['connections'] ){
c_name = v['connections'][c]['name'];
found = false;
for (i in r['connections']){
if (r['connections'][i]['name'] == c_name){
r['connections'][i]['appears'] += 1;
found = true;
}
}
if (found == false){
var con = {'name':c_name,'appears':1};
r['connections'].push(con);
}
};
r.weight += v.weight;
});
return r;
}
This then resulted in records of the type desired:
{
"_id" : "Skaundee",
"value" : {
"author" : "Skaundee",
"connections" : [
{
"name" : "Carnage",
"appears" : 1
},
{
"name" : "Tree",
"appears" : 1
}
],
"weight" : 2
}
}