This question already has an answer here:
jQuery sort array value in sequence
(1 answer)
Closed 3 years ago.
Suppose I have an array like this:
let array = ["eid_x", "eid_x", "cid_x", "cid_x"]
how would I sort it so it's like this?
let array = ["cid_x", "eid_x", "cid_x", "eid_x"]
The original array is in a random order, example: eid, cid, cid, eid.
Does anyone know how this can be sorted like in the second array?
Split the items into two arrays then grab an item off of one alternating between the two arrays within your loop (or an Array#map in this case).
let array = ["eid_x", "eid_x", "cid_x", "cid_x"]
let eid = array.filter(i => i == 'eid_x')
let cid = array.filter(i => i == 'cid_x')
let result = new Array(array.length).fill(null)
.map((i, idx) => idx % 2 == 0 ? cid.shift() : eid.shift())
console.log(result)
Related
This question already has answers here:
Get all unique values in a JavaScript array (remove duplicates)
(91 answers)
Closed 12 months ago.
How can i check if an array has element twice and log only the element that is not.
const duplicateElements = (array) => {
for(let numbers of array) {
// code here
}
}
const numbers = [1,3,2,4,1,3,2];
duplicateElements(numbers);
// Output 4
With the JS function filter you can archive this. First you have to iterate your array. then check with the filter function how many times the current value is inside your array. If equal 1 then push to an result array.
const d = [];
const arr = [1,3,2,4,1,3,2]
arr.forEach((e) => {
if (arr.filter(x => x == e).length === 1) {
d.push(e);
}
})
console.log(d);
This question already has answers here:
Simplest code for array intersection in javascript
(40 answers)
Closed last year.
how do i filter an array from the given values of another array? The newCampaignId varable is the one i want to filter through. I do it like so:
const newCampaignId = ["101","102","103","104"] // not used
const newCampaign = subscriptions.filter((sub) => sub.id == "101" || sub.id == "102" || sub.id =="103" || sub.id == "104");
But i am sure there is an easier way of doing this. Any ideas?
You can use Array.includes property. You can read more about it on https://www.w3schools.com/jsref/jsref_includes_array.asp
const newCampaign = subscriptions.filter(sub => newCampaignId.includes(sub.id));
This question already has answers here:
How to get a key in a JavaScript object by its value?
(31 answers)
Closed 3 years ago.
I have array of object like this :
I need to select only the array which has coreSystemID = 'T24' so I tried credsArr.find() it shows my .find is not a function.
below is my code:
var creds = credsArr.find(x => x.coreSystemID === 'T24')
And I should use the index value like this - credsArr[3053], because the index may vary so I need to select the array which has coreSystemID = 'T24'
let keys = Oject.keys(credsArr).filter(key => credsArr[key].coreSystemID==='T24')
keys will have all the keys of credsArr having credsArr[key].coreSystemID as 'T24'
let systemT24 = credsArr[keys[0]] will give you the object you are looking for given that there is only one object with coreSystemID as 'T24'.
If you have multiple objects that might have coreSystemID as 'T24' you can map it to a new array like this:
let systemT24 = keys.map(key => credsArr[key])
You can use Object.values(credsArr):
var creds = Object.values(credsArr).find(x => x.coreSystemID === 'T24');
or if you need key of the object:
var creds = Object.entries(credsArr).find(x => x[1].coreSystemID === 'T24');
let key = creds[0];
let value = creds[1];
This question already has answers here:
How to sum elements at the same index in array of arrays into a single array?
(7 answers)
Closed 3 years ago.
I am working with a multi-dimensional array.
the following is my array:
let arr = [[1,2,3],[1,2,3],[1,2,3]];
the length of the inner arrays will always be the same.
I want create a function to add all the arrays elements together with their respective elements and create a new array with the result.
so my desired output is
result =[3,6,9];
You can use nested for loop for that.
let arr = [[1,2,3],[1,2,3],[1,2,3]];
let res = Array(arr[0].length).fill(0);
for(let i = 0;i<arr[0].length;i++){
for(let j = 0;j<arr.length;j++){
res[i] += arr[j][i]
}
}
console.log(res)
This question already has answers here:
Getting a list of associative array keys
(6 answers)
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
I need to group the rows out of a table that has a matching order number, and then iterate over the groupings.
I have this code working which is creating the perfect array, data-wise:
var multItems = [];
// Combine items under orders,
$('tr.order').each(function(){
var orderNum = $(this).find('.ordernumber').val();
if ( ($('tr.order .ordernumber[value="' + orderNum + '"]').length > 1 ) && !(orderNum in multItems) ){
$('tr.order .ordernumber[value="' + orderNum + '"]').each(function(){
if (!(orderNum in multItems)){
multItems[orderNum] = [];
}
multItems[orderNum].push(this);
});
}
});
// Create new tr with order totals (of each item)
for (var i = multItems.length - 1; i >= 0; i--) {
// Code
};
But it creates an array with a length of 0, apparently, where multItems = [], but multItems[orderNumber] is defined... just with no way to access it if I don't know the order numbers.
I could make an array of the order numbers separately, but that feels like it must be the long way round. If I just create a numbered array, how do I know which number to pop the items from the orders into?
With your current code you have
var orderNum = $(this).find('.ordernumber').val();
where val() returns a string and not a number. So when you are doing multItems[orderNum] it is a string.
For the current code to work, you want to use a for in loop.
for (var prop in multItems) {
if( multItems.hasOwnProperty( prop ) ) {
console.log(multItems[prop]);
}
}
FYI: Order is not guaranteed. Also you should be using an object {} and not an array here.
Now the other thing you can do is to use parseInt to change the string into a number and than magically your for loop would start working. [This is assuming that ordernumber is a numeric value]
var orderNum = parseInt($(this).find('.ordernumber').val(), 10);