Javascript extend Array so it duplicates and reverses - javascript

If I have an array like [1,2,3,4], and I want to duplicate and reverse it, how do I get it to return [1,2,3,4,4,3,2,1]?
Array.prototype.duplicateAndReverse = function() {
const initial = this;
const reversed = initial.reverse();
return initial.concat(reversed);
}
What am i doing wrong here? It returns [4,3,2,1,4,3,2,1]

Try this:
Array.prototype.duplicateAndReverse = function() {
const initial = this;
const reversed = initial.slice().reverse();
return initial.concat(reversed);
}
var myArray = [1,2,3,4];
alert(myArray.duplicateAndReverse());
Your code is reversing initial as well as setting reversed to the result, so you have two identical (reversed) arrays. Instead, use .slice() to duplicate the initial array, and reverse that instead.

var arr = [1,2,3,4];
for(var len = arr.length; len; len--)
arr.push(arr[len - 1]);
console.log(arr);
I don't recommend you mess with Array.prototype but here is what you want:
Array.prototype.duplicateAndReverse = function() {
let res = this.slice(); // create another copy so the original array will stay intact
for(var len = res.length; len; len--)
res.push(res[len - 1]);
return res;
}
let arr = [1, 2, 3, 4];
console.log(arr.duplicateAndReverse());

The problem was that you tried to reverse the initial array in place.Use the following optimized solution:
Array.prototype.duplicateAndReverse = function() {
return this.concat(this.slice().reverse());
}
console.log([1,2,3,4].duplicateAndReverse());
To retain the original array and create its reversed copy use Array.prototype.slice() function combined with Array.prototype.reverse() function

This is your problem because reverse() also reverse the original array
arr
var arr = [1,2,3,4]
var dup = arr.reverse()
console.log(arr) // => [4, 3, 2, 1]
console.log(dup) // => [4, 3, 2, 1]
You need to clone the original array first
var dup = arr.slice().reverse()
And then you can concat the 2 arrays to get the result. Happy coding!

Oriental solution, hihi.
var arr = [1,2,3,4];
var newArr = [];
arr.forEach((_,i) => newArr.unshift(arr[arr.length-i-1]) && newArr.push(arr[arr.length-i-1]))
console.log(newArr);

Related

Can I modify a for...of loop's variable? [duplicate]

I'm trying to square each number in an array and my original code didn't work. I looked up another way to do it, but I'd like to know WHY the original code didn't work.
Original code:
function(arr) {
ret= [];
for (var i = 0, len = arr.length; i < len; i++) {
root = Math.sqrt(arr[i]);
ret.push(root);
}
return ret;
}
Working Code:
function(arr) {
ret= [];
for (var i = 0, len = arr.length; i < len; i++) {
ret.push(arr[i] * arr[i]);
}
return ret;
}
Math.sqrt gives you square root not square of a number. Use Math.pow with second argument of 2.
How about that ?
function (arr) {
return arr.map(function (x) {
return Math.pow(x, 2);
});
}
Array.map(func) applies the function to each element of the map and returns the array composed of the new values.
Math.pow(base, exp) raises base to its exp power.
The first sample is taking the square root, not squaring the value. To square you want to use
Math.pow(arr[i],2);
Here is how it can be done, using a simple method called .forEach
var numbers = [1,2,3,4,5,6,7,8];
numbers.forEach(function(element, index, array){
array[index] = element* element;
});
console.log(numbers);
Best way to Square each number in an array in javascript
Array.prototype.square = function () {
var arr1 = [];
this.map(function (obj) {
arr1.push(obj * obj);
});
return arr1;
}
arr = [1, 6, 7, 9];
console.log(arr.square());
arr1 = [4, 6, 3, 2];
console.log(arr1.square())
Here is the function write with ES6 Exponentiation (**):
let arr = [1, 6, 7, 9];
let result = arr.map(x => x ** 2);
console.log(result);
The original code is taking the square root of the value. The second version is multiplying the value with itself (squaring it). These are inverse operations
I hope this answers your question
const numbers = [1,2,3,4,5,6,7,8,9];
for(let squareIt of numbers){
console.log(Math.pow(squareIt, 2));
}
Resolved by kiss-barnabas
Use embedded for , for pretty syntax :
var arr=[1,2,3,4] ;
[for (i of arr) i*i ];
//OUT : > [1,4,9,16]
Declarative Programming :)
let list = [1,2,3,4,5,6,7,8,9,10];
let result = list.map(x => x*x);
console.log(result);
Avoid unnecessary loops, use map()function
let array = [1,2,3,4,5];
function square(a){ // function to find square
return a*a;
}
arrSquare = array.map(square); //array is the array of numbers and arrSquare will be an array of same length with squares of every number
You can make the code shorter like this:
let array = [1,2,3,4,5];
arrSquare = array.map(function (a){return a*a;});
let arr = [1, 2, 3];
let mapped = arr.map(x => Math.pow(x, 2));
console.log(mapped);
This should work.
let kiss=[3,4,5,6];
let arra=[];
for(o in kiss){
arra.push(kiss[o]*kiss[o])
}
console.log(kiss=arra)
using array square root of the first number is equal to the cube root of the second number
function SquareAndCube(arr) {
let newarr = [];
for(i=0 ; i<arr.length; i++) {
if(Math.sqrt(arr[0])/2 === Math.cbrt(arr[1])/2 ) {
return true;
} else return false;
}
}
console.log(SquareAndCube([36, 215]))
This should work with the .map() method:
const number = [2, 6, 6, 2, 8, 10];
const squareNumber = number.map(num => num*num);
console.log(squareNumber);
function squareDigits(num){
//may the code be with you
var output = [];
var splitNum = num.toString();
for(var i = 0; i < splitNum.length; i++){
output.push(splitNum.charAt(i))
}
function mapOut(){
var arr = output;
return arr.map(function(x){
console.log(Math.pow(x, 2));
})
}
mapOut();
}
squareDigits(9819);
This should work
This will work
const marr = [1,2,3,4,5,6,7,8,9,10];
console.log(marr.map((x) => Math.pow(x, 2)));
function map(square,a) {
var result = [];
for(var i=0;i<=a.length-1;i++)
result[i]=square(a[i]);
return result;
}
var square = function(x) {
return x*x;
}
var value=[1,2,3,4];
var final= map(square,value);
console.log(final);
You can also try the above code snippet.

Array transformation/manipulation

I have one array like this one:
array1=[{value:1, label:'value1'},{value:2, label:'value2'}, {value:3, label:'value3'}]
I have a second array of integer :
array2=[1,3]
I would like to obtain this array without a loop for :
arrayResult = ['value1', 'value3']
Does someone know how to do it with javascript ?
Thanks in advance
Map the elements in array2 to the label property of the element in array1 with the corresponding value:
array2 // Take array2 and
.map( // map
function(n) { // each element n in it to
return array1 // the result of taking array1
.find( // and finding
function(e) { // elements
return // for which
e.value // the value property
=== // is the same as
n; // the element from array2
}
)
.label // and taking the label property of that elt
;
}
)
;
Without comments, and in ES6:
array.map(n => array1.find(e => e.value === n).label);
You can use .filter and .map, like this
var array1 = [
{value:1, label:'value1'},{value:2, label:'value2'}, {value:3, label:'value3'}
];
var array2 = [1, 3];
var arrayResult = array1.filter(function (el) {
return array2.indexOf(el.value) >= 0;
}).map(function (el) {
return el.label;
});
console.log(arrayResult);
A simple for-loop should suffice for this. In the future you should seriously post some code to show what you have tried.
var array1=[{value:1, label:'value1'},{value:2, label:'value2'}, {value:3, label:'value3'}];
var array2=[1,3];
var result = [];
for (var i = 0; i < array2.length; i++){
result.push(array1[array2[i]-1].label);
}
console.log(result); //["value1", "value3"]
JSBIN
Good answers all. If I may suggest one more alternative using Map as this seems to be suited to a key:value pair solution.
var arr1 = [ {value:1, label:'value1'}, {value:2, label:'value2'}, {value:3, label:'value3'} ];
var arr2 = [1, 3];
// create a Map of the first array making value the key.
var map = new Map( arr1.map ( a => [a.value, a.label] ) );
// map second array with the values of the matching keys
var result = arr2.map( n => map.get ( n ) );
Of course this supposes that the key:value structure of the first array will not become more complex, and could be written in the simpler form of.
var arr1 = [[1,'value1'], [2,'value2'], [3,'value3']]; // no need for names
var arr2 = [1, 3];
var map = new Map( arr1 ); // no need to arr1.map ;
var result = arr2.map( n => map.get ( n ) );
Just index the first array using the _.indexBy function:
var indexedArray1 = _.indexBy(array1, "value");
_.map(array2, function(x) { return indexedArray1[x].label; });

Integer arrays comparison

I have a question of JS arrays.
Example:
var fullArr = [1,2,3,4];
var partArr = [2,3];
var newArr = [];
We have a main array fullArr and a partial array partarr. I want to create a function/filter, which is looking for existing items in fullArr and not in partArr.
In this example above newArr must be equal to [1,4].
I've tried doing something like this, but it's not working properly.
for (var k in fullArray) { // [1,2,3,4]
for (var j in selectedArray) { // [1,4]
if (fullArray[k] == selectedArray[j]) {
newArray.splice(selectedArray[j] - 1, 1); // must be [2,3]
break;
}
}
}
What is a good way of making this? Thanks.
Here's one
var newArr = fullArr.filter(function(f) { // The filter() method creates a new array with all elements that pass the test implemented by the provided function.
return partArr.indexOf(f) == -1; // The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.
})
to impress the girls, you can also
var newArr = fullArr.filter(function(f) {
return !~partArr.indexOf(f);
})
Here is the code for your requirement.
var fullArr = [1,2,3,4];
var partArr = [2,3];
var newArr = [];
for(var i=0;i<fullArr.length;i++){
if(partArr.indexOf(fullArr[i]) == -1)
newArr.push(fullArr[i]);
};
Here is the working Link
Hope it works :)
In fact, you want a common part between arrays. Obviously you can choose splice or indexOf to have O(n * m) or even O(m * n^2) performance. It's obviously suboptimal for any array larger than few elements
Or you can use objects as hash maps to find differences in (in worst case) O(n + m log m):
var fullArr = [1,2,3,4];
var partArr = [2,3];
var temporaryObject = Object.create(null);
partArr.forEach(el=>temporaryObject[el] = true); // temporaryObject after this operation is {"2": true, "3": true}
var newArr = fullArr.filter(el=>temporaryObject[el]);
In this example I have used ES6 feature called "arrow functions". It translates to following ES5 code:
var partArr = [2, 3];
var temporaryObject = Object.create(null);
partArr.forEach(function (el) {
temporaryObject[el] = true;
}); // temporaryObject after this operation is {"2": true, "3": true}
var newArr = fullArr.filter(function (el) {
return temporaryObject[el];
});
You can use the filter() function that works on arrays:
var newArr = fullArr.filter(function(val, i, arr) {
return partArr.indexOf(val) === -1;
});
This will return a new array containing the values of every iteration that returns true.
Should you ever need to do this on an object in the future a great way is to first convert the object keys to an array and then run the filter:
Object.keys(myObj).function(val, i, arr) {
return partArr.indexOf(val) === -1;
});
Here are few other approaches:
var fullArr = [1,2,3,4];
var partArr = [2,3];
var newArr = [];
1.
fullArr.map(function(element){
if(partArr.indexOf(element) === -1) newArr.push(element);
})
console.log(newArr);
2.
for(i in fullArr){
if(partArr.indexOf(fullArr[i]) === -1) newArr.push(fullArr[i]);
}
console.log(newArr);
3.
fullArr.forEach(function(element){
if(partArr.indexOf(element) === -1) newArr.push(element);
})
console.log(newArr);

Finding nested duplicate arrays in JavaScript. (Nested Array uniq in lodash/underscore)

I am trying to determine if an array of JavaScript arrays contains duplicates. Is this possible? I am first trying to see if I can strip the duplicates out and then do an equality check but I cannot get past the first part. Here is what underscore returns:
var arr1 = [[1,2], [2,3], [1,2]];
var arr2 = _.uniq(arr1);
var arraysAreEqual = _.isEqual(arr1, arr2);
console.log(arraysAreEqual, arr1, arr2);
// true
Jsbin: http://jsbin.com/vogumo/1/edit?js,console
Anyone know of a way to determine if the array contains duplicate arrays?
It's a little sloppy, but (possible)
var arr2 = _.uniq(arr1, function(item) {
return JSON.stringify(item);
});
will give you a correct result
Try This:
var numArray = [1, 7, 3, 0, 9, 7, 8, 6, 2, 3];
var duplicates = [];
var sortednumArray = numArray.sort();
for (var i = 0; i < sortednumArray.length; i++) {
//console.log(sortednumArray[i]);
if (sortednumArray[i] == sortednumArray[i + 1]) {
duplicates.push(sortednumArray[i]);
}
}
if (duplicates.length == 0) {
console.log("Soted Array:");
for(var i = 0; i < sortednumArray.length; i++) {
console.log(sortednumArray[i]);
}
} else {
console.log("Duplicates:");
for(var i = 0; i < duplicates.length; i++){
console.log(duplicates[i]);
}
}
Program pushes all duplicates to an array called 'duplicates' then displays it, but if none are present, it displays the sorted version of numArray
From the underscore.js documentation:
uniq _.uniq(array, [isSorted], [iteratee]) Alias: unique
Produces a
duplicate-free version of the array, using === to test object
equality. If you know in advance that the array is sorted, passing
true for isSorted will run a much faster algorithm. If you want to
compute unique items based on a transformation, pass an iteratee
function.
But arrays can't be strictly compared in JavaScript.
Therefore, you can use a transformation function to enable comparison with uniq. For example:
console.log([1,2] === [1,2]) // false, can't strict compare arrays
console.log([1,2].toString()) // "1,2" - string representation
console.log([1,2].toString() === [1,2].toString()) // true, strings can be compared
var valueToString = function(v) {return v.toString()}; // transform array to string
var arr1 = [[1,2], [2,3], [1,2]];
var arr2 = _.uniq(arr1, false, valueToString); // compare based on transformation
var arraysAreEqual = _.isEqual(arr1, arr2);
console.log("arraysAreEqual:", arraysAreEqual, arr1, arr2);
// false
// [[1, 2], [2, 3], [1, 2]]
// [[1, 2], [2, 3]]
Note that transforming to string is "hacky": you would be better off comparing each value of the array, as discussed in this StackOverflow question.
By using the proposed equals implementation in that question, you would need to implement your own version of uniq that uses equals instead of ===.
The implementation of uniq in Underscore is very straight-forward - it creates a new result array and loops through the given array. If the current value is not already in result, insert it.
console.log("Using array comparison:");
arrayEquals = function (array1, array2) {
// if any array is a falsy value, return
if (!array1 || !array2)
return false;
// compare lengths - can save a lot of time
if (array1.length != array2.length)
return false;
for (var i = 0, l=array1.length; i < l; i++) {
// Check if we have nested arrays
if (array1[i] instanceof Array && array2[i] instanceof Array) {
// recurse into the nested arrays
if (!arrayEquals(array1[i],array2[i]))
return false;
}
else if (array1[i] !== array2[i]) {
return false;
}
}
return true;
};
_.uniqArrays = function(array) {
if (array == null) return [];
var result = [];
for (var i = 0, length = array.length; i < length; i++) {
var value = array[i];
var arrayEqualsToValue = arrayEquals.bind(this, value); // arrayEquals with first argument set to value
var existing = _.find(result, arrayEqualsToValue); // did we already find this?
if (!existing) {
result.push(value);
}
}
return result;
};
var arr3 = _.uniqArrays(arr1);
arraysAreEqual = _.isEqual(arr1, arr3);
console.log("arraysAreEqual:", arraysAreEqual, arr1, arr3); // false
I made a jsbin with all the code, if you want to play around.
In the latest lodash (4.6.1) you could do something like this:
if (_.uniqWith(arr, _.isEqual).length < arr.length) {
// then there were duplicates
}

Javascript merge 2 arrays and sum same key values

I have 2 array:
var array1 = [[5,10],[6,10],[7,10],[8,10],[9,10]];
var array2 = [[1,10],[2,10],[3,10],[4,10],[5,40],[6,40]];
Want to get 1 merged array with the sum of corresponding keys;
var array1 = [[1,10],[2,10],[3,10],[4,10],[5,50],[6,50],[7,10],[8,10],[9,10]];
Both arrays have unique keys, but the corresponding keys needs to be summed.
I tried loops, concat, etc but can't get the result i need.
anybody done this before?
You can use .reduce() to pass along an object that tracks the found sets, and does the addition.
DEMO: http://jsfiddle.net/aUXLV/
var array1 = [[5,10],[6,10],[7,10],[8,10],[9,10]];
var array2 = [[1,10],[2,10],[3,10],[4,10],[5,40],[6,40]];
var result =
array1.concat(array2)
.reduce(function(ob, ar) {
if (!(ar[0] in ob.nums)) {
ob.nums[ar[0]] = ar
ob.result.push(ar)
} else
ob.nums[ar[0]][1] += ar[1]
return ob
}, {nums:{}, result:[]}).result
If you need the result to be sorted, then add this to the end:
.sort(function(a,b) {
return a[0] - b[0];
})
This is one way to do it:
var sums = {}; // will keep a map of number => sum
// for each input array (insert as many as you like)
[array1, array2].forEach(function(array) {
//for each pair in that array
array.forEach(function(pair) {
// increase the appropriate sum
sums[pair[0]] = pair[1] + (sums[pair[0]] || 0);
});
});
// now transform the object sums back into an array of pairs
var results = [];
for(var key in sums) {
results.push([key, sums[key]]);
}
See it in action.
a short routine can be coded using [].map()
var array1 = [[5,10],[6,10],[7,10],[8,10],[9,10]];
var array2 = [[1,10],[2,10],[3,10],[4,10],[5,40],[6,40]];
array1=array2.concat(array1).map(function(a){
var v=this[a[0]]=this[a[0]]||[a[0]];
v[1]=(v[1]||0)+a[1];
return this;
},[])[0].slice(1);
alert(JSON.stringify(array1));
//shows: [[1,10],[2,10],[3,10],[4,10],[5,50],[6,50],[7,10],[8,10],[9,10]]
i like how it's just 3 line of code, doesn't need any internal function calls like push() or sort() or even an if() statement.
Try this:
var array1 = [[5,10],[6,10],[7,10],[8,10],[9,10]];
var array2 = [[1,10],[2,10],[3,10],[4,10],[5,40],[6,40]];
var res = [];
someReasonableName(array1, res);
someReasonableName(array2, res);
function someReasonableName(arr, res) {
var arrLen = arr.length
, i = 0
;
for(i; i < arrLen; i++) {
var ar = arr[i]
, index = ar[0]
, value = ar[1]
;
if(!res[index]) {
res[index] = [index, 0];
}
res[index][1] += value;
}
}
console.log(JSON.stringify(res, null, 2));
So, the result may have holes. Just like 0th index. Use the below function if you want to ensure there are no holes.
function compact(arr) {
var i = 0
, arrLen = arr.length
, res = []
;
for(i; i < arrLen; i++) {
var v = arr[i]
;
if(v) {
res[res.length] = v;
}
}
return res;
}
So, you can do:
var holesRemoved = compact(res);
And finally if you don't want the 0th elem of res. Do res.shift();
Disclaimer: I am not good with giving reasonable names.
The simple solution is like this.
function sumArrays(...arrays) {
const n = arrays.reduce((max, xs) => Math.max(max, xs.length), 0);
const result = Array.from({ length: n });
return result.map((_, i) => arrays.map(xs => xs[i] || 0).reduce((sum, x) => sum + x, 0));
}
console.log(...sumArrays([0, 1, 2], [1, 2, 3, 4], [1, 2])); // 2 5 5 4

Categories