Javascript number of object by different property values in array - javascript

Best way to count the number of objects in a array with different object property p values.
function([{"p":"a"},{"p":"b"},{"p":"a"}]){
// some code
}
// in this case return 2

You can use Array.prototype.filter() to keep values that you want. In this case, you must create a variable temp for storing duplicate values and also within the filter function returns true if it does not exist in case. So you have a new array of unique values.
var arr = [{"p":"a"},{"p":"b"},{"p":"a"}], temp = [];
var arrUniques = arr.filter(function(obj){
return temp.indexOf(obj.p) === -1 ? !!temp.push(obj.p) : false
});
alert(arrUniques.length)

With a Map:
var props = new Map();
for (var i = 0; i < array.length; i++) {
var prop = array[i].p,
count = props.get(prop) || 0;
props.set(prop, count + 1);
}
var size = props.size;
If your properties can be safely casted to strings, you can use a common object:
var props = {};
...
var size = Object.keys(props).length;
Otherwise, Map is your answer.

function getUniquePropertyCount(a, p) {
return a.reduce(function (res, el) {
!~res.indexOf(el[p]) && res.push(el[p]);
return res;
}, []).length;
}
document.write(getUniquePropertyCount([{ "p": "a" }, { "p": "b" }, { "p": "a" }], 'p'));

I suppose this is one of those questions where if you ask four programmers, you'll get five answers.
The other answers so far show some interesting approaches. I would watch out for browser compatibility; for example Map() is only available in the newest browsers, not in IE 10 or prior.
So here's one more solution. It's a bit more code, but it's pretty easy to understand, and it works in every browser:
function countUniqueProperties( key, array ) {
var count = 0, values = {};
for( var i = 0; i < array.length; ++i ) {
var value = array[i][key];
if( ! Object.prototype.hasOwnProperty.call( values, value) ) {
++count;
values[value] = true;
}
}
return count;
}
countUniqueProperties( 'p', [ {p:'a'}, {p:'b'}, {p:'a'} ] );
The one complicated part here is the Object.prototype.hasOwnProperty.call(values,value). You could just use values.hasOwnProperty(value), but that would fail if one of your property values was the string "hasOwnProperty":
countUniqueProperties( 'p', [ {p:'a'}, {p:'hasOwnProperty'}, {p:'a'} ] );
Using the longer form avoids this issue.

lodash is nice for things like this.
Use
_.uniq([{"p":"a"},{"p":"b"},{"p":"a"}]).length
and it'll return 2.

Related

Arrays to objects

I have this question and I cannot find the answer:
Create a function called firstAndLast which takes an array and returns an object which has one property. The key of that property should be the first array element and it's value should be the last element in the array. Example:
firstAndLast(["queen", "referee", "cat", "beyonce"]) should return {queen: "beyonce"}.
Some diversity of approaches (admittedly esoteric, but fun!):
function firstAndLast(a, o){
return !(o = {}, o[a.shift()] = a.pop()) || o;
}
console.log(firstAndLast([1,2,3,4,5]));
console.log(firstAndLast(['a','to','z']));
https://jsfiddle.net/brtsbLp1/
And, of course:
function firstAndLast(a, o){
return !(o = o || {}, o[a.shift()] = a.pop()) || o;
}
console.log(firstAndLast(['a','to','z']));
console.log(firstAndLast(['a','to','z'], {a:'nother',obj:'ect'}));
https://jsfiddle.net/brtsbLp1/1/
Another fun one:
function firstAndLast(a){
return JSON.parse('{"'+a.shift()+'":"'+a.pop()+'"}');
}
https://jsfiddle.net/brtsbLp1/2/
That last one will choke on the first being a number (since labels aren't allowed to be numbers only), plus other issues in general. But this should give some food for thought. The other answers are a bit more obvious.
you can specify the key with []
function firstAndLast(arr){
let o = {};
o[arr[0]] = arr[arr.length - 1]; // the key is the first elem, the value the last elem of the array
return o;
}
let a = [1,2,3,4,5]
let b = firstAndLast(a)
function firstAndLast(arr){
var obj = {
}
var length = arr.length;
obj[arr [0]]=arr[length-1] //{queen:beyonce}
return obj
}
var arr = ["queen", "referee", "cat", "beyonce"]
var output = firstAndLast(arr) //{queen:beyonce}

Get object names from a array of objects in javascript

i cannot find a solution of my issue. i probably making things more complicated.
here is the array:
var Statistics = {
"STAT 1 COLUMNS" : ["Date","Group Name","Product Name","Version","Version Date","Status","Total","Total L0","Total L1","Total L2","To be validated","Created","Updated"],
"STAT 2 TITLE" : ["12/12/2013","Led Zeppelin","roofies","V5.3","2013-08-13 ","ACCEPTED <br/>(2013-9-10)","774","334","3","437","20","57","102"],
"STAT 3 TITLE" : ["22/11/2014","Deep Purple","upper","V1","2006-01-01 "," ","3","1","0","2","1"," "," "],
...
}
i would like to get the object name (such as 'STAT 1 COLUMNS', 'STAT 2 TITLE', 'STAT 3 TITLE'..) for a specific object, by specifing the index.
for instance, givemepleasethenameoftheobject('2') -> "STAT 3 TITLE"
can you please give me a hand?
Try with:
function givemepleasethenameoftheobject(index, data) {
var i = 0;
for ( var k in data ) {
if ( index == i++ ) {
return k;
}
}
}
var result = givemepleasethenameoftheobject(2, Statistics);
This should achieve what you need:
function getStatisticName(index)
{
var count = 0;
for(var i in Statistics)
{
if(count == index)
return i;
count++;
}
}
See this jsFiddle
Try the 'Object.keys();' function.
Object.keys(Statistics); // ['STAT 1 COLUMNS', 'STAT 2 TITLE', 'STAT 3 TITLE'];
You can loop through the members of an object like this:
for (var key in Statistics) {
console.log(key);
}
However, you can not reliably access them by index like that. The members in an object is not guaranteed to remain in the same order that you create them, and different browsers actually do order the members differently.
If you want to access the member names by index in a predictable way, you would need to get all names and then sort them:
function givemepleasethenameoftheobject(index) {
var keys = [];
for (var key in Statistics) {
keys.push(key);
}
keys.sort();
return keys[index];
}
If you don't need to be compatible with older browsers (e.g. Internet Explorer 8), you can use the Object.Keys method:
function givemepleasethenameoftheobject(index) {
var keys = Object.keys(Statistics);
keys.sort();
return keys[index];
}
Object keys order is not guaranteed in JavaScript, so gimmeTheKey(Statistics, 2) is not guaranteed to return "STAT 3 TITLE".
However:
function gimmeTheKey( obj, idx ){
return Object.keys( obj )[idx];
}

Declaring array of objects

I have a variable which is an array and I want every element of the array to act as an object by default. To achieve this, I can do something like this in my code.
var sample = new Array();
sample[0] = new Object();
sample[1] = new Object();
This works fine, but I don't want to mention any index number. I want all elements of my array to be an object. How do I declare or initialize it?
var sample = new Array();
sample[] = new Object();
I tried the above code but it doesn't work. How do I initialize an array of objects without using an index number?
Use array.push() to add an item to the end of the array.
var sample = new Array();
sample.push(new Object());
To do this n times use a for loop.
var n = 100;
var sample = new Array();
for (var i = 0; i < n; i++)
sample.push(new Object());
Note that you can also substitute new Array() with [] and new Object() with {} so it becomes:
var n = 100;
var sample = [];
for (var i = 0; i < n; i++)
sample.push({});
Depending on what you mean by declaring, you can try using object literals in an array literal:
var sample = [{}, {}, {} /*, ... */];
EDIT: If your goal is an array whose undefined items are empty object literals by default, you can write a small utility function:
function getDefaultObjectAt(array, index)
{
return array[index] = array[index] || {};
}
Then use it like this:
var sample = [];
var obj = getDefaultObjectAt(sample, 0); // {} returned and stored at index 0.
Or even:
getDefaultObjectAt(sample, 1).prop = "val"; // { prop: "val" } stored at index 1.
Of course, direct assignment to the return value of getDefaultObjectAt() will not work, so you cannot write:
getDefaultObjectAt(sample, 2) = { prop: "val" };
You can use fill().
let arr = new Array(5).fill('lol');
let arr2 = new Array(5).fill({ test: 'a' });
// or if you want different objects
let arr3 = new Array(5).fill().map((_, i) => ({ id: i }));
Will create an array of 5 items. Then you can use forEach for example.
arr.forEach(str => console.log(str));
Note that when doing new Array(5) it's just an object with length 5 and the array is empty. When you use fill() you fill each individual spot with whatever you want.
After seeing how you responded in the comments. It seems like it would be best to use push as others have suggested. This way you don't need to know the indices, but you can still add to the array.
var arr = [];
function funcInJsFile() {
// Do Stuff
var obj = {x: 54, y: 10};
arr.push(obj);
}
In this case, every time you use that function, it will push a new object into the array.
You don't really need to create blank Objects ever. You can't do anything with them. Just add your working objects to the sample as needed. Use push as Daniel Imms suggested, and use literals as Frédéric Hamidi suggested. You seem to want to program Javascript like C.
var samples = []; /* If you have no data to put in yet. */
/* Later, probably in a callback method with computed data */
/* replacing the constants. */
samples.push(new Sample(1, 2, 3)); /* Assuming Sample is an object. */
/* or */
samples.push({id: 23, chemical: "NO2", ppm: 1.4}); /* Object literal. */
I believe using new Array(10) creates an array with 10 undefined elements.
You can instantiate an array of "object type" in one line like this (just replace new Object() with your object):
var elements = 1000;
var MyArray = Array.apply(null, Array(elements)).map(function () { return new Object(); });
Well array.length should do the trick or not? something like, i mean you don't need to know the index range if you just read it..
var arrayContainingObjects = [];
for (var i = 0; i < arrayContainingYourItems.length; i++){
arrayContainingObjects.push {(property: arrayContainingYourItems[i])};
}
Maybe i didn't understand your Question correctly, but you should be able to get the length of your Array this way and transforming them into objects. Daniel kind of gave the same answer to be honest. You could just save your array-length in to his variable and it would be done.
IF and this should not happen in my opinion you can't get your Array-length. As you said w/o getting the index number you could do it like this:
var arrayContainingObjects = [];
for (;;){
try{
arrayContainingObjects.push {(property: arrayContainingYourItems[i])};
}
}
catch(err){
break;
}
It is the not-nice version of the one above but the loop would execute until you "run" out of the index range.
//making array of book object
var books = [];
var new_book = {id: "book1", name: "twilight", category: "Movies", price: 10};
books.push(new_book);
new_book = {id: "book2", name: "The_call", category: "Movies", price: 17};
books.push(new_book);
console.log(books[0].id);
console.log(books[0].name);
console.log(books[0].category);
console.log(books[0].price);
// also we have array of albums
var albums = []
var new_album = {id: "album1", name: "Ahla w Ahla", category: "Music", price: 15};
albums.push(new_album);
new_album = {id: "album2", name: "El-leila", category: "Music", price: 29};
albums.push(new_album);
//Now, content [0] contains all books & content[1] contains all albums
var content = [];
content.push(books);
content.push(albums);
var my_books = content[0];
var my_albums = content[1];
console.log(my_books[0].name);
console.log(my_books[1].name);
console.log(my_albums[0].name);
console.log(my_albums[1].name);
This Example Works with me.
Snapshot for the Output on Browser Console
Try this-
var arr = [];
arr.push({});
const sample = [];
list.forEach(element => {
const item = {} as { name: string, description: string };
item.name= element.name;
item.description= element.description;
sample.push(item);
});
return sample;
Anyone try this.. and suggest something.
Use array.push() to add an item to the end of the array.
var sample = new Array();
sample.push(new Object());
you can use it
var x = 100;
var sample = [];
for(let i=0; i<x ;i++){
sample.push({})
OR
sample.push(new Object())
}
Using forEach we can store data in case we have already data we want to do some business login on data.
var sample = new Array();
var x = 10;
var sample = [1,2,3,4,5,6,7,8,9];
var data = [];
sample.forEach(function(item){
data.push(item);
})
document.write(data);
Example by using simple for loop
var data = [];
for(var i = 0 ; i < 10 ; i++){
data.push(i);
}
document.write(data);
If you want all elements inside an array to be objects, you can use of JavaScript Proxy to apply a validation on objects before you insert them in an array. It's quite simple,
const arr = new Proxy(new Array(), {
set(target, key, value) {
if ((value !== null && typeof value === 'object') || key === 'length') {
return Reflect.set(...arguments);
} else {
throw new Error('Only objects are allowed');
}
}
});
Now if you try to do something like this:
arr[0] = 'Hello World'; // Error
It will throw an error. However if you insert an object, it will be allowed:
arr[0] = {}; // Allowed
For more details on Proxies please refer to this link:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy
If you are looking for a polyfill implementation you can checkout this link:
https://github.com/GoogleChrome/proxy-polyfill
The below code from my project maybe it good for you
reCalculateDetailSummary(updateMode: boolean) {
var summaryList: any = [];
var list: any;
if (updateMode) { list = this.state.pageParams.data.chargeDefinitionList }
else {
list = this.state.chargeDefinitionList;
}
list.forEach((item: any) => {
if (summaryList == null || summaryList.length == 0) {
var obj = {
chargeClassification: item.classfication,
totalChargeAmount: item.chargeAmount
};
summaryList.push(obj);
} else {
if (summaryList.find((x: any) => x.chargeClassification == item.classfication)) {
summaryList.find((x: any) => x.chargeClassification == item.classfication)
.totalChargeAmount += item.chargeAmount;
}
}
});
if (summaryList != null && summaryList.length != 0) {
summaryList.push({
chargeClassification: 'Total',
totalChargeAmount: summaryList.reduce((a: any, b: any) => a + b).totalChargeAmount
})
}
this.setState({ detailSummaryList: summaryList });
}
var ArrayofObjects = [{}]; //An empty array of objects.

how to prevent adding duplicate keys to a javascript array

I found a lot of related questions with answers talking about for...in loops and using hasOwnProperty but nothing I do works properly. All I want to do is check whether or not a key exists in an array and if not, add it.
I start with an empty array then add keys as the page is scrubbed with jQuery.
Initially, I hoped that something simple like the following would work: (using generic names)
if (!array[key])
array[key] = value;
No go. Followed it up with:
for (var in array) {
if (!array.hasOwnProperty(var))
array[key] = value;
}
Also tried:
if (array.hasOwnProperty(key) == false)
array[key] = value;
None of this has worked. Either nothing is pushed to the array or what I try is no better than simply declaring array[key] = value Why is something so simple so difficult to do. Any ideas to make this work?
Generally speaking, this is better accomplished with an object instead since JavaScript doesn't really have associative arrays:
var foo = { bar: 0 };
Then use in to check for a key:
if ( !( 'bar' in foo ) ) {
foo['bar'] = 42;
}
As was rightly pointed out in the comments below, this method is useful only when your keys will be strings, or items that can be represented as strings (such as numbers).
var a = [1,2,3], b = [4,1,5,2];
b.forEach(function(value){
if (a.indexOf(value)==-1) a.push(value);
});
console.log(a);
// [1, 2, 3, 4, 5]
For more details read up on Array.indexOf.
If you want to rely on jQuery, instead use jQuery.inArray:
$.each(b,function(value){
if ($.inArray(value,a)==-1) a.push(value);
});
If all your values are simply and uniquely representable as strings, however, you should use an Object instead of an Array, for a potentially massive speed increase (as described in the answer by #JonathanSampson).
A better alternative is provided in ES6 using Sets. So, instead of declaring Arrays, it is recommended to use Sets if you need to have an array that shouldn't add duplicates.
var array = new Set();
array.add(1);
array.add(2);
array.add(3);
console.log(array);
// Prints: Set(3) {1, 2, 3}
array.add(2); // does not add any new element
console.log(array);
// Still Prints: Set(3) {1, 2, 3}
If you're already using spread...
let colors = ['red', 'orange', 'yellow'];
let moreColors = ['orange', 'green'];
let mergedColors = [...colors, ...moreColors];
and want to avoid duplicates...
let mergedColors = [...colors, ...moreColors.filter(c => !colors.includes(c)) ];
You can try this:
var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];
var uniqueNames = [];
$.each(names, function(i, el){
if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
});
Easiest way to find duplicate values in a JavaScript array
The logic is wrong. Consider this:
x = ["a","b","c"]
x[0] // "a"
x["0"] // "a"
0 in x // true
"0" in x // true
x.hasOwnProperty(0) // true
x.hasOwnProperty("0") // true
There is no reason to loop to check for key (or indices for arrays) existence. Now, values are a different story...
Happy coding
function check (list){
var foundRepeatingValue = false;
var newList = [];
for(i=0;i<list.length;i++){
var thisValue = list[i];
if(i>0){
if(newList.indexOf(thisValue)>-1){
foundRepeatingValue = true;
console.log("getting repeated");
return true;
}
} newList.push(thisValue);
} return false;
}
var list1 = ["dse","dfg","dse"];
check(list1);
Output:
getting repeated
true
let x = "farceus";
let y = "character";
const commonCharacters = function (string1, string2) {
let duplicateCharacter = "";
for (let i = 0; i < string1.length; i += 1) {
if (duplicateCharacter.indexOf(string1[i]) === -1) {
if (string2.indexOf(string1[i]) !== -1) {
duplicateCharacter += string1[i];
}
}
}
return [...duplicateCharacter];
};
console.log(commonCharacters(x, y));

In a recursive function, where to store results?

I'm currently trying to let JavaScript generate a truth table for a boolean function. Given a function, the code should just list all boolean combinations possible, with the function output from each combination.
As for generating all combinations, I put this together (simplified to the combinations code only):
var table = [];
function combinations(current) {
var current = current || [];
if(current.length === 3) {
table.push(current);
} else {
var c = copy(current);
c.push(true);
combinations(c);
c = copy(current);
c.push(false);
combinations(c);
}
}
function copy(a) {
var r = [];
for(var i = 0; i < a.length; i++) r.push(a[i]);
return r;
}
combinations(); // now table consists of each pair of 3 boolean values
So basically when it has reached a combination (i.e. current.length === 3), it pushes a result record to table. I was wondering, however, whether this is the recommended way of storing results of a recursive function.
I faced the recommendation of using return inside the recursive function, but how would one implement such a thing - i.e., if combinations in the end has to return an array containing all elements, how is it possible to do so? I could, of course, just use return table in the end, but I'm actually looking for a way to do this all inside the function, without an external variable like now.
So, how do I make combinations return the results as an array without using an external variable?
Use Array.concat().
function combinations(current) {
var current = current || [];
if(current.length === 3) {
return [current];
} else {
return combinations(current.concat(true)).concat(combinations(current.concat(false)));
}
}
var table = combinations(); // now table consists of each pair of 3 boolean values
console.log(table);
Much more elegant, no?
Demo →
To avoid polluting the global space you could use a closure to contain your recursive function. There is an excellent writeup of this concept at http://drewwells.net/blog/2010/recursion-in-javascript/.
Your current solution seems fine to me. It might not be the most elegant, but it is simple and does the job (the only ugly bit is the hardcoded 3 - you should turn that into a parameter)
Your real question seems to be more language-agnostic than Javascript. If you want the function to return the combinations, than you can prefectly do so, just clearly have in mind what your function should return and write the base and recursive cases:
function combinations(domain, n){
//returns a list of combinations of length `n` with elements from `domain`
if(n <= 0){
return [[]]; //the empty combination is the only solution
}else{
var small_combs = combinations(domain, n-1);
var big_combs = [];
for(var i=0; i<domain.length; i++){
for(var j=0; j<small_combs.length; j++){
big_combs.push(small_combs[j].concat(domain[i]))
}
}
return big_combs;
}
}
table = combinations([true, false], 3);
var id = { "object": "page", "entry": [{ "id": "1588811284674233", "time": 1511177084837, "messaging": [{ "sender": { "id": "1393377930761248" }, "recipient": { "id": "1588811284674233" }, "timestamp": 1511177084553, "message": { "mid": "mid.$cAAX_9pLcfu1mCnGmiVf2Sxd2erI2", "seq": 1882, "text": "a" } }] }] };
function getKey(obj, data) {
var data = data || [];
if (obj) {
var keys = Object.keys(obj);
for (var pos in keys) {
console.log();
data.push(keys[pos]);
if ((obj[keys[pos]].constructor === Array)) {
for (var i = 0; i < obj[keys[pos]].length; i++) {
getKey(obj[keys[pos]][i], data);
}
}
else if (obj[keys[pos]].constructor === Object) {
getKey(obj[keys[pos]], data);
}
}
return data;
}
}

Categories

Resources