How to remove an array from a nested array in JavaScript? [duplicate] - javascript

This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
Closed 1 year ago.
Is there a way to remove an array from a nested array in JavaScript?
I have the following array:
arr = [
[1, 2],
[2, 3],
[3, 4]
]
and I want to remove the value [2, 3] from the array so that it results with:
arr = [
[1, 2],
[3, 4]
]
I have already tried the answers on How can I remove a specific item from an array?, but they don't seem to work. I was wondering if there was a fast efficient way of doing this.
Edit:
I already tried using indexOf and findIndex, and it does not return the index of an array inside of the array.
arr = [
[1, 2],
[2, 3],
[3, 4]
];
console.log(arr.indexOf([2, 3]));
console.log(arr.findIndex([2, 3]));
This did not work, even though it was suggested in the comments below.
Furthermore, using:
console.log(arr.filter(nested => nested[0] !== 2 || nested[1] !== 3));
will be inefficient as in my code I need to remove large lists, which have hundreds of values, I only provided an example in my question.
Any help would be appreciated.

var arr = [
[1, 2],
[2, 3],
[3, 4]
]
console.log('Before =>', arr);
arr.splice(1, 1); // .splice(index, 1);
console.log('After=>', arr);

Related

get the number of inputs given to a function js

Assume i have created a function function findInputGiven(){} and i called it somewhere below twice findInputGiven([1, 2], [3, 4]), findInputGiven([1, 2], [3, 4], [5, 6]). This function can be called with multiple inputs, i dont know how many inputs will be available in my findInputGiven function. How can i get the number of inputs given to call function.
My task is to return an array of all numbers of given arrays. If the function is called like findInputGiven([1, 2], [5, 6]), then i should return [1, 2, 5, 6].
My task is to return an array of all numbers of given arrays
What you can do is use rest parameters to get an array of all arguments and then return the flattened result. You can still get a count of the arguments if required but for this task, I don't think you'll need it.
function findInputGiven(...arrays) {
// you can get the argument count if you need it
console.log(`Got ${arrays.length} argument(s)`)
// return the flattened result
return arrays.flat()
}
console.info(findInputGiven([1, 2], [3, 4]))
console.info(findInputGiven([1, 2], [3, 4], [5, 6]))
.as-console-wrapper { max-height: 100% !important }
If you are not familiar with Rest parameters, Array.prototype.flat of ES6 feature along with don't want to "transpile" your code.
You can achieve it by using arguments.
function findInputGiven() {
const numberOfInputs = arguments.length;
console.log(`Number of inputs: ${numberOfInputs}`);
let result = [];
Object.values(arguments).forEach(function(item, index){
if(Array.isArray(item))
result = result.concat(item);
else
result.push(item);
});
return result;
}
console.log(findInputGiven([1, 2], [3, 4]))
console.log(findInputGiven([1, 2], [3, 4], [5, 6], 7))
More detailed explanation:
As you may know, rest parameters & flat() aren't supporting IE. So that is where transpiler come from :)

How to subtract one array from another, in javascript [duplicate]

This question already has answers here:
How to get the difference between two arrays in JavaScript?
(84 answers)
Closed 2 years ago.
If I have an array A = [1, 2, 3, 4, 5] and B = [3, 4, 5] I want to return a new array with values
[1, 2]. remove same value.
Using Array.prototype.includes, you can check if B contains A item or not.
And using Array.prototype.filter, you can get the filtered values which are not included in array B.
const A = [1, 2, 3, 4, 5];
const B = [3, 4, 5];
const output = A.filter((item) => !B.includes(item));
console.log(output);

How can I get an array with a nested array whose elements have a unique order?

I have an array of arrays. How can I get an array with a nested array whose elements have a unique order?
What I have:
[[1, 2], [1, 2, 3], [3, 4]]
Expected Output:
[[1, 2, 3], [3, 4]]
I think, that I can format to JSON, and get my result with filter and indexOf ,I have tried this:
var unique = x2.map(ar=>JSON.stringify(ar).replace(/[\[\]']+/g,''))
.filter((itm, idx, arr) => arr.indexOf(itm) === idx);
Here is my try, I first sorted the array by its length then applied filter and match the index position by making use of every and includes. Here is an example:
var example = [[1, 2, 35, 7], [1, 2, 3], [35, 7], [3, 4], [4]];
var result = example.sort((a,b)=>b.length-a.length).filter((k,i,self)=>self.findIndex(p=>k.every(l=>p.includes(l)))==i);
console.log(result);
I hope this helps.

Filter an array returns empty

I'm trying to populate an array with all values retrieved (I am in a loop) and then to delete values already present in the yearsJustSelected array:
yearsJustSelected.forEach((el:any)=>{
yearsJustSelectedAll.push(el);
});
yearsJustSelectedFiltered = yearsJustSelected.filter((el: any) => !yearsJustSelectedAll.includes(el));
For example:
loop 1 --> yearsJustSelected=[1, 2, 3] - yearsJustSelectedAll=[1, 2, 3] - yearsJustSelectedFiltered = []
loop 2 --> yearsJustSelected=[2, 3, 4, 5] - yearsJustSelectedAll=[1, 2, 3, 4, 5] - yearsJustSelectedFiltered=[4, 5] --> because [2, 3 ] were already present in the yearsJustSelectedAll in the first loop
This code:
yearsJustSelectedFiltered = yearsJustSelected.filter((el: any) => !yearsJustSelectedAll.includes(el));
always return an empty array.
yearsJustSelected.forEach((el:any)=>{
yearsJustSelectedAll.push(el);
});
// The code above will add all elements in yearsJustSelected into yearsJustSelectedAll
// making the two arrays the same.
There are no instances where one array has elements that are not in the other array
You could check if the target array does not have the item. Then push the item to the target array.
function select(source, target) {
source.forEach(v => target.includes(v) || target.push(v));
}
var yearsJustSelectedAll = [];
select([1, 2, 3], yearsJustSelectedAll);
console.log(...yearsJustSelectedAll);
select([2, 3, 4, 5], yearsJustSelectedAll);
console.log(...yearsJustSelectedAll);
select([4, 5], yearsJustSelectedAll);
console.log(...yearsJustSelectedAll);

How to iterate over array in reverse order with Lodash?

I want to iterate over an array in reverse order using Lodash. Is that possible?
My code looks like below.
var array = [1, 2, 3, 4, 5];
_.each(array, function(i) {
_.remove(array, i);
});
When I do _.pullAt(array, 0) new array is [2, 3, 4, 5]. All array elements shifted to left by 1 position, and current index is pointing to element 3. After next iteration, 3 will get deleted and then 5. After 3rd iteration array contains [2, 4] which I couldn't delete.
If I iterate over the array in reverse order, then this problem won't arise. In Lodash, can I iterate over an array in reverse order?
You can use _.reverse available in version 4:
var array = [1, 2, 3, 4, 5];
array = _.reverse(array)
console.log(array)
//5, 4, 3, 2, 1
See How do I empty an array in JavaScript? if you only want that and choose your weapon.
Otherwise, strictly answering the question, to iterate the indices from the length of the array to zero, you could use the "down-to" --> operator1. But that's not really necessary, even underscore isn't necessary in this case as the .pop function is enough.
var arr = [1, 2, 3, 4, 5],
index = arr.length;
while (index --> 0) {
console.log(index);
arr.pop();
}
console.log(arr);
If you're using Lodash like the functions referenced in the question seem to indicate, you could use _.eachRight.
var arr = [1, 2, 3, 4, 5];
_.eachRight(arr, function(value) {
console.log(value);
arr.pop();
});
console.log(arr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script>
1 The down-to operator doesn't exist and is only a -- decrement followed by a > comparison.

Categories