javascript remove duplicates from array of arrays - javascript

How to remove duplicates on an array like below:
arr = [
[828, 187, 1, 14, 1, 25, 1417608935730],
[828, 187, 1, 14, 1, 25, 1417608935740],
[828, 187, 1, 14, 1, 25, 1417608935750],
[828, 187, 1, 14, 1, 25, 1417608935760],
[600, 578, 1, 14, 1, 25, 1417608935760],
[828, 187, 1, 14, 1, 25, 1417608935780],
[828, 187, 1, 14, 1, 25, 1417608935790]
]
Since 4th and 5th array have 1417608935760 similar value I want to delete next duplicate and keep only one.
I tried this but not working:
$.each(arr, function(i, insidearr){
if($.inArray(insidearr[6], uniqueNames) === -1)
uniqueNames.push(insidearr);
});
Pls help. Thanks in advance

You're pushing the entire row into uniqueNames. The array will never be equal to the number at the end of each row, so the $.inArray() test will always fail. You need separate arrays for just the element you're comparing and the resulting array.
var newArray = [];
var uniqueNames = {};
$.each(arr, function(i, insidearr) {
if (!(insidearr[6] in uniqueNames)) {
newArray.push(insidearr);
uniqueNames[insidearr[6]] = true;
}
});
I used an object rather than an array for uniqueNames, because searching for an object key is faster than scanning an array.

Just try with:
var uniqueArr = $.grep(arr, function(insidearr){
if ($.inArray(insidearr[6], uniqueNames) === -1) {
uniqueNames.push(insidearr[6]);
return true;
}
});

try this one:
var arr = [
[828, 187, 1, 14, 1, 25, 1417608935730],
[828, 187, 1, 14, 1, 25, 1417608935740],
[828, 187, 1, 14, 1, 25, 1417608935750],
[828, 187, 1, 14, 1, 25, 1417608935760],
[600, 578, 1, 14, 1, 25, 1417608935760],
[828, 187, 1, 14, 1, 25, 1417608935780],
[828, 187, 1, 14, 1, 25, 1417608935790]
];
var len = arr.length;
while(--len) {
if(arr[len][6] == arr[len-1][6]) {
arr.splice(len-1, 1)
}
}
console.log(arr); // here is what you want
If you don't want to manipulate arr, you just need to clone the array by var newArr = arr.slice()

Related

How does this for in Loop Work to Sort Numbers using Quick Sort in JavaScript

How does the for in loop work below to correctly sort the numbers in the left array and right array for the quick sort implementation below?
I'd like an example of a first example iteration of the sortArr(left) recursive call, then another iteration, and so on (maybe up to three or four iterations).
Then I'll be able to understand the sortArr(left), including the sortArr(right) recursive call.
const unsortedArr = [31, 27, 28, 42, 13, 8, 11, 30, 17, 41, 15, 43, 1, 36, 9, 16, 20, 35, 48, 37, 7, 26, 34, 21, 22, 6, 29, 32, 49, 10, 12, 19, 24, 38, 5, 14, 44, 40, 3, 50, 46, 25, 18, 33, 47, 4, 45, 39, 23, 2];
let sortArr = (arr) => {
if (arr.length < 2) return arr;
const pivot = arr[Math.floor(arr.length / 2)];
let left = [];
let equal = [];
let right = [];
equal.push(pivot);
for (let element of arr) {
if (element < pivot) {
left.push(element);
} else if (element > pivot) {
right.push(element);
}
}
let sortedArr = sortArr(left)
.concat(equal)
.concat(sortArr(right));
return sortedArr;
};
console.log(sortArr(unsortedArr));

Javascript Json : Sum values by matching json keys

My input in one single json
input = [{
"201609": 0,
"201610": 0,
"201611": 0,
"201804": 130,
"201805": 130,
"fy16Q3": 17,
"fy17Q1": 0,
"fy17": 0,
"fy17Q2": 0,
"fy17Q3": 0
}, {
"201510": 0,
"201610": 0,
"201611": 10,
"201803": 20,
"201804": 30,
"201805": 40,
"201806": 130,
"201809": 130,
"fy17Q1": 2,
"fy17": 3,
"fy17Q2": 5,
"fy17Q3": 6
}];
In the output i want to iterate through all the elements of this json and sum the values of the matching keys. Also keep the non matching lone keys in the output as well.
output =
[{
"201510": 5, // no matching pair
"201609": 3, // no matching pair
"201610": 6+9 = 15, // matching pair exist
"201611": 10+12 = 22,
"201803": 20,
"201804": 30+13 = 33,
"201805": 40+14 = 44,
"201806": 130,
"201809": 130,
"fy16Q3": 17, // no matching pair
"fy17Q1": 2+7 = 9, // matching pair exist
"fy17": 3+8 = 11,
"fy17Q2": 5+9 = 14,
"fy17Q3": 6+100 = 106
}];
The problem is that iam not able to figure out how to handle the keys which don't have a matching pair.
You can try the following code. Your desired output looks different than your logic
var data = input = [{
"201609": 0,
"201610": 0,
"201611": 0,
"201804": 130,
"201805": 130,
"fy16Q3": 17,
"fy17Q1": 0,
"fy17": 0,
"fy17Q2": 0,
"fy17Q3": 0
}, {
"201510": 0,
"201610": 0,
"201611": 10,
"201803": 20,
"201804": 30,
"201805": 40,
"201806": 130,
"201809": 130,
"fy17Q1": 2,
"fy17": 3,
"fy17Q2": 5,
"fy17Q3": 6
}];
var output = data.reduce((arr,d,x) =>{
var keys = Object.keys(d);
keys.forEach( (k) => {
if(!arr[k]) arr[k] = 0;
arr[k] = arr[k] + d[k];
})
return arr;
},{});
console.log(output);
input = [{
"201609": 0,
"201610": 0,
"201611": 0,
"201804": 130,
"201805": 130,
"fy16Q3": 17,
"fy17Q1": 0,
"fy17": 0,
"fy17Q2": 0,
"fy17Q3": 0
}, {
"201510": 0,
"201610": 0,
"201611": 10,
"201803": 20,
"201804": 30,
"201805": 40,
"201806": 130,
"201809": 130,
"fy17Q1": 2,
"fy17": 3,
"fy17Q2": 5,
"fy17Q3": 6
}];
var output = input.reduce((p,c) => {
for(let k in c){
p[k] = (p[k] || 0) + c[k];
}
return p;
},{});
console.log(output);
input = [{
"201609": 0,
"201610": 0,
"201611": 0,
"201804": 130,
"201805": 130,
"fy16Q3": 17,
"fy17Q1": 0,
"fy17": 0,
"fy17Q2": 0,
"fy17Q3": 0
}, {
"201510": 0,
"201610": 0,
"201611": 10,
"201803": 20,
"201804": 30,
"201805": 40,
"201806": 130,
"201809": 130,
"fy17Q1": 2,
"fy17": 3,
"fy17Q2": 5,
"fy17Q3": 6
}];
var output = [{}];
for( i in input){
for (key in input[i]){
if(output[0].hasOwnProperty(key)){
output[0][key]+=input[i][key];
}else{
output[0][key]=input[i][key];
}
}
}
console.log(output)
Use array reduce method. In this method take the first object of the input array as the initial object since.SInce object keys are always unique, for any matching key just update the value
var input = [{
"201609": 0,
"201610": 0,
"201611": 0,
"201804": 130,
"201805": 130,
"fy16Q3": 17,
"fy17Q1": 0,
"fy17": 0,
"fy17Q2": 0,
"fy17Q3": 3
}, {
"201510": 0,
"201610": 0,
"201611": 10,
"201803": 20,
"201804": 30,
"201805": 40,
"201806": 130,
"201809": 130,
"fy17Q1": 2,
"fy17": 3,
"fy17Q2": 5,
"fy17Q3": 6
}];
// the array will start reducing from second element that is
// element from index 1
let toLoopArray = input.slice(1, input.length);
let output = input.reduce(function(acc, curr) {
// for the current object check if the key already exist
// if not then create the new key and update value
for (let keys in curr) {
if (!acc.hasOwnProperty(keys)) {
acc[keys] = curr[keys]
} else {
// acc[keys] = acc[keys] + curr[keys]
console.log(curr[keys])
acc[keys] = acc[keys] + curr[keys]
}
}
return acc;
}, input[0])
console.log([output])

Why does my replace methods throws an error?

I want to replace the number in the myArray to either 'even' or 'odd' but it throws an error that TypeError: val.replace is not a function
const myArray = [
[23, 156, 25, 10, 52, 23],
[12, 100, 23, 56, 81, 93],
[42.5, 71, 10, 23, 35, 11, 72, 99],
[11, 100, 99, 102, 13, 8, 12]
];
let arr = myArray.map(item => {
return item.map(val => {
if (val % 2 == 0) {
val.toString();
val.replace(val, "even");
} else {
val.replace(val, "odd");
}
});
});
console.log(arr); //TypeError: val.replace is not a function
You need to return the new value.
String#replace returns a new string with the replaced values, but you do not have a strings here.
const myArray = [
[23, 156, 25, 10, 52, 23],
[12, 100, 23, 56, 81, 93],
[42.5, 71, 10, 23, 35, 11, 72, 99],
[11, 100, 99, 102, 13, 8, 12]
];
let arr = myArray.map(item => {
return item.map(val => {
if (val % 2 == 0) {
return "even";
} else {
return "odd";
}
});
});
console.log(arr);
You don't need to use replace, you simply need to map
const myArray = [
[23, 156, 25, 10, 52, 23],
[12, 100, 23, 56, 81, 93],
[42.5, 71, 10, 23, 35, 11, 72, 99],
[11, 100, 99, 102, 13, 8, 12]
];
let arr = myArray.map(item => item.map(val => val % 2 == 0 ? 'even' : 'odd'))
console.log(arr);
You can try following (Check for value if divisible by 0, if yes return even else odd)
const myArray = [[23, 156, 25, 10, 52, 23], [12, 100, 23, 56, 81, 93], [42.5, 71, 10, 23, 35, 11, 72, 99], [11, 100, 99, 102, 13, 8, 12]];
let arr = myArray.map(item => item.map(val => val%2 === 0 ? "even": "odd"));
console.log(arr);
Or you can improve your code as follows
const myArray = [[23, 156, 25, 10, 52, 23], [12, 100, 23, 56, 81, 93], [42.5, 71, 10, 23, 35, 11, 72, 99], [11, 100, 99, 102, 13, 8, 12]];
let arr = myArray.map(item => {
return item.map(val => {
if (val % 2 == 0) {
val = val.toString(); // you need to reassign after toString
val = val.replace(val, "even"); // you need to reassign after replace
} else {
val = val.toString(); // you need to reassign after toString
val = val.replace(val, "odd"); // you need to reassign after replace
}
return val; // finally you need to return the updated value
});
});
console.log(arr); //Now the right input will be logged

Issue with custom array ordering sequence Javascript

I am facing an issue with array ordering sequence. Need your help for desired result.
var a = [0, 1, 2, 3, 4, 5, 6, 77, 7, 8, 9, 10, 11, 12, 35, 36, 43, 51, 72, 89, 95, 100];
var b = [6,5,7,8,0,800,46,1,2,3,12,47,100,95];
var c = [];
for (var i = 0; i <= (a.length) - 1; i++) {
var res = b.indexOf(a[i]);
if (res > -1) {
c.push(a[i]);
}
}
document.write(c);
// I need same sequence of array B in reponse
// Desired Result
// 6,5,7,8,0,1,2,3,12,100,95
Iterate array b instead of a:
var a = [0, 1, 2, 3, 4, 5, 6, 77, 7, 8, 9, 10, 11, 12, 35, 36, 43, 51, 72, 89, 95, 100];
var b = [6,5,7,8,0,800,46,1,2,3,12,47,100,95];
var c = [];
for (var i = 0; i < b.length; i++) {
var res = a.indexOf(b[i]);
if (res > -1) {
c.push(b[i]);
}
}
console.log(c.join(','));
A more functional solution is to use Array#filter on b:
var a = [0, 1, 2, 3, 4, 5, 6, 77, 7, 8, 9, 10, 11, 12, 35, 36, 43, 51, 72, 89, 95, 100];
var b = [6,5,7,8,0,800,46,1,2,3,12,47,100,95];
var c = b.filter(function(n) {
return a.indexOf(n) !== -1;
});
console.log(c.join(','));

Working With Multi Functional Array In Javascript

jsfiddle with all the data which crashes and doesn't run.
jsfiddle with less data which runs but if you
console.log(`row ${g}, col ${i}`);
console.log(obj[pastWinners]);
It shows it is undefined.
I have a multi dimensional array in Javascript which runs some calculations for me. This array gets converted to a object literal and then after some data manipulation is changed to a json object and eventually a string. The program works great!
I have to add 310 more lines to the multi dimensional array. When I do I get this error,
index.html:331 Uncaught TypeError: Cannot read property '0' of
undefined
The original array looks like this,
var pastWinners = [
[2, 9, 19, 23, 38, 40],
[17, 25, 31, 35, 38, 43],
[8, 10, 17, 30, 33, 43],
[10, 17, 26, 28, 36, 43],
[14, 20, 25, 28, 34, 41],
[8, 13, 21, 23, 25, 43],
[10, 11, 18, 24, 27, 30],
[21, 22, 23, 26, 33, 39],
[6, 21, 23, 29, 36, 40],
[10, 12, 16, 21, 25, 42],
[1, 11, 20, 27, 34, 37]
];
Notice the single digit numbers do not have a 0 in the tens place. But the new data looks like this,
[05, 07, 17, 18, 33, 35],
It does have a 0 in the tens place. I mention this because from aside from it being 310 more lines to the array that is the only difference. I have checked the data 50 times and there is nothing wrong with it.
Would a small difference like that make a difference to cause an error like this? It says the error is getting throws at line 331 in my program,
var obj = {};
for (var g = 0; g < pastWinners.length; g++) {
for (var i = 0, j = pastWinners.length; i < j; i++) {
if (obj[pastWinners[g][i]] == undefined) {
console.log(`row ${g}, col ${i}`);
console.log(obj[pastWinners]);
}
if (obj[pastWinners[g][i]]) { //this is line 331
obj[pastWinners[g][i]]++;
} else {
obj[pastWinners[g][i]] = 1;
}
}
}
var picks = [];
for (var numbs in obj) {
picks.push([numbs, obj[numbs]])
picks.sort(
function(a, b) {
return a[1] - b[1]
}
)
}
picks.reverse();
var topPicks = []
for (var winners = 0; winners < 6; winners++) {
topPicks.push(picks[winners][0]);
}
var weekPicks = topPicks.toString();
console.log(weekPicks);
alert(weekPicks);
Does anyone see any reason this would happen when I add this data in? If it is an issue of the size of the array how can I handle this because I have a few thousand more lines of data to add in.
row 0, col 0
index.html:333 undefined
index.html:332 row 0, col 1
index.html:333 undefined
index.html:332 row 0, col 2
index.html:333 undefined
index.html:332 row 0, col 3
index.html:333 undefined
index.html:332 row 0, col 4
index.html:333 undefined
index.html:332 row 0, col 5
index.html:333 undefined
index.html:332 row 0, col 6
index.html:333 undefined
By adding a console.log(g) of what index you were up to when the error happens, this is actually an issue with your data:
[2, 7, 8, 9, 27, 38],
[9, 12, 17, 24, 26, 41]
[18, 20, 21, 31, 40, 44],
You are missing a comma after the 176th index. https://jsfiddle.net/6h7bedtL/1/
Try to print the array in the console when you get undefined and check your array size in the log.
for (var g = 0; g < pastWinners.length; g++) {
for (var i = 0, j = pastWinners.length; i < j; i++) {
if (obj[pastWinners[g][i]] == undefined){
console.log(`row ${g}, col ${i}`);
console.log(obj[pastWinners]);
}
if (obj[pastWinners[g][i]]) { //this is line 331
obj[pastWinners[g][i]]++;
} else {
obj[pastWinners[g][i]] = 1;
}
}
}
I would do it differently:
a. change the second loop...
b. define obj
var pastWinners = [
[2, 9, 19, 23, 38, 40],
[17, 25, 31, 35, 38, 43],
[8, 10, 17, 30, 33, 43],
[10, 17, 26, 28, 36, 43],
[14, 20, 25, 28, 34, 41],
[8, 13, 21, 23, 25, 43],
[10, 11, 18, 24, 27, 30],
[21, 22, 23, 26, 33, 39],
[6, 21, 23, 29, 36, 40],
[10, 12, 16, 21, 25, 42],
[1, 11, 20, 27, 34, 37],
];
var obj=[];
for (var g = 0; g < pastWinners.length; g++) {
for (var i = 0; i < pastWinners[g].length; i++) {
if (obj[pastWinners[g][i]]) { //this is line 331
obj[pastWinners[g][i]]++;
} else {
obj[pastWinners[g][i]] = 1;
}
}
}
console.log(obj)
Also you should check for the existence of pastWinners[g] before you loop through it or try to access a child array using
if (pastWinners[g] != null) {
The reason for this is that if pastWinners[g] for some reason is not defined, then your script will attempt to do this:
undefined[i] or get the value of [i] from undefined -- which it cant because undefined is not an array -- which breaks your script
var pastWinners = [
[2, 9, 19, 23, 38, 40],
[17, 25, 31, 35, 38, 43],
[8, 10, 17, 30, 33, 43],
[10, 17, 26, 28, 36, 43],
[14, 20, 25, 28, 34, 41],
[8, 13, 21, 23, 25, 43],
[10, 11, 18, 24, 27, 30],
[21, 22, 23, 26, 33, 39],
[6, 21, 23, 29, 36, 40],
[10, 12, 16, 21, 25, 42],
[1, 11, 20, 27, 34, 37],
];
for (var g = 0; g < pastWinners.length; g++) {
if (pastWinners[g] != null) { // check for valid item
for (var i = 0, j = pastWinners[g].length; i < j; i++) {
if (pastWinners[g][i] != null) {
pastWinners[g][i]++;
} else {
pastWinners[g][i] = 1;
}
}
}
}
console.log(pastWinners);
Possibly
for (var i = 0, j = pastWinners.length; i < j; i++)
should be
for (var i = 0, j = pastWinners[g].length; i < j; i++)
The array items in pastWinners do not have the length of pastwinners :D

Categories

Resources