Can I use hasOwnProperty() on an array? I have this array of RGBA values:
colors = [[240,120,120,255], [125,222,111,255], [9,56,237,255], [240,120,120,255], [240,120,120,255], [240,120,120,255]]
I'd like to create an object that sorts them in order of their frequency in the array. I'm trying a bination of things.
First I thought to convert the array into an object using:
function toObject(arr) {
var rv = {};
for (var i = 0; i < arr.length; ++i){
rv[i] = arr[i];
}
//console.log('the array is now this ', rv)
return rv;
}
But that returns something like this:
{0: [240,120,120,255], 1:[125,222,111,255],2:[9,56,237,255], 3:[240,120,120,255], 4:[240,120,120,255], 5:[240,120,120,255]}
So I'm wondering if I can call hasOwnProperty on it like this?
function reduceColors(passedArray){
var empty = {}
return passedArray.filter(function(item){
return empty.hasOwnProperty(item["value"])
})
}
Can I use hasOwnProperty() on an array? I have this array of RGBA values:
colors = [[240,120,120,255], [125,222,111,255], [9,56,237,255], [240,120,120,255], [240,120,120,255], [240,120,120,255]]
I'd like to create an object that sorts them in order of their frequency in the array. I'm trying a bination of things.
First I thought to convert the array into an object using:
function toObject(arr) {
var rv = {};
for (var i = 0; i < arr.length; ++i){
rv[i] = arr[i];
}
//console.log('the array is now this ', rv)
return rv;
}
But that returns something like this:
{0: [240,120,120,255], 1:[125,222,111,255],2:[9,56,237,255], 3:[240,120,120,255], 4:[240,120,120,255], 5:[240,120,120,255]}
So I'm wondering if I can call hasOwnProperty on it like this?
function reduceColors(passedArray){
var empty = {}
return passedArray.filter(function(item){
return empty.hasOwnProperty(item["value"])
})
}
Share
Improve this question
edited Oct 6, 2020 at 14:29
Peter Mortensen
31.6k22 gold badges110 silver badges133 bronze badges
asked Oct 2, 2016 at 22:36
EJWEJW
6142 gold badges10 silver badges23 bronze badges
4
-
Is expected result that elements having most elements in mon in any order themselves with other sub-arrays, or exact sequence match? That is, expected result would be
[240,120,120,255],[240,120,120,255],[240,120,120,255],[240,120,120,255]
followed by either[125,222,111,255],[9,56,237,255]
or[9,56,237,255], [125,222,111,255]
? – guest271314 Commented Oct 2, 2016 at 22:41 -
First things first; in JS
[240,120,120,255] === [240,120,120,255] // <- false
– Redu Commented Oct 2, 2016 at 22:44 - exact sequence match, because these are [r,g,b,a] colors values. – EJW Commented Oct 2, 2016 at 23:07
- "sorts them in order of their frequency" ... tantalizing endeavor and something puters can be great at! algorithms to the rescue. – sova Commented Oct 2, 2016 at 23:20
2 Answers
Reset to default 1You can use hashing for this. With hashing you can group the arrays that have the same values in the same orders and you can count their frequency.
Here is an example:
var colors = [[240,120,120,255], [125,222,111,255], [9,56,237,255], [240,120,120,255], [240,120,120,255], [240,120,120,255]];
var hashed = [];
colors.forEach(function(arr){
var id = hash(arr);
var contains = hashed.find(v => v.id == id);
if(contains){
contains.count++;
}else{
hashed.push({id:id, color:arr, count:1});
}
});
hashed.sort(function(a,b){return b.count - a.count;})
console.log(hashed);
function hash(arr){
var hash = "#";
arr.forEach(function(v){
hash+=v.toString(16);
});
return hash;
}
In this example, I'm creating the hash by converting the RGBA values to hexadecimal numbers, but a better solution will be to store them like that (in hexadecimal), so you will have a one-dimensional array instead of a two-dimensional array.
You can use a for
loop, Array.prototype.filter()
, Array.prototype.every()
, and the strict equality operator to check if each array within colors
contains each element of a sibling array within colors
at same index; concatenate each matched sibling array to an output array
colors = [
[240, 120, 120, 255],
[125, 222, 111, 255],
[9, 56, 237, 255],
[240, 120, 120, 255],
[240, 120, 120, 255],
[240, 120, 120, 255]
]
var res = [];
for (var i = 0; i < colors.length; i++) {
if (res.length < colors.length) {
[...res] = [...res,
...colors.filter(function(el) {
return el.every(function(elem, index) {
return elem === colors[i][index]
})
})
]
} else {
break;
}
}
console.log(res);