Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an array like this
var myArray = [
"J20J205147902_B_B716202",
"R20Q205147902_F_B716202",
"P20W205147_902_alternate1",
"M20K205147_902_alternate4",
"F20G205147_902_alternate3",
"K20J205147_902_alternate2",
"L20H205147_902_main"];
I want to sort this array by this pattern
var map = {
"_F_":1,
"_main":2,
"_alternate1":3,
"_alternate2":4,
"_alternate3":5,
"_alternate4":6,
"_alternate5":7,
"_B_":8
};
So, the output should be like this
"R20Q205147902_F_B716202",
"L20H205147_902_main",
"P20W205147_902_alternate1",
"K20J205147_902_alternate2",
"F20G205147_902_alternate3",
"M20K205147_902_alternate4",
"J20J205147902_B_B716202"
You can use a custom sorting comparator function as follows:
var myArray = [
"J20J205147902_B_B716202",
"R20Q205147902_F_B716202",
"P20W205147_902_alternate1",
"M20K205147_902_alternate4",
"F20G205147_902_alternate3",
"K20J205147_902_alternate2",
"L20H205147_902_main"];
var map = {
"_F_":1,
"_main":2,
"_alternate1":3,
"_alternate2":4,
"_alternate3":5,
"_alternate4":6,
"_alternate5":7,
"_B_":8
}
myArray.sort((...args) => {
const [a, b] = args.map(str => Object.keys(map).find(key => str.includes(key)))
return map[a] - map[b];
})
console.log(myArray)
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 months ago.
Improve this question
// I wanted to create a variable to hold the month and a variable that hold the number...
var finances = [
['Jan-1999', 867886],
['Feb-1999', 984653],
['Mar-1999', 322012],
['Apr-1999', -69417],
['May-1999', 310503],
['Jun-1999', 522857],
['Jul-1999', 1033096],
['Aug-1999', 604885],
['Sep-1999', -216386],
['Oct-1999', 477532],
['Nov-1999', 893810]];
What you need is map() that returns a new array with the result of a function applied to your initial array. In this case your functions would either select the first element of the subarray (the date), or the second element (the number).
var finances = [
['Jan-1999', 867886],
['Feb-1999', 984653],
['Mar-1999', 322012],
['Apr-1999', -69417],
['May-1999', 310503],
['Jun-1999', 522857],
['Jul-1999', 1033096],
['Aug-1999', 604885],
['Sep-1999', -216386],
['Oct-1999', 477532],
['Nov-1999', 893810]];
const dates = finances.map((e) => e[0]);
const numbers = finances.map((e) => e[1]);
console.log(dates);
console.log(numbers);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
I am fairly new to Javascript is there a way in which we can achieve the following result?
const bikes = ["honda","kawasaki",suzuki]
The main object array can be divided into equal parts based on the number of bikes, like the first few rows with the main object will be "honda", the next few groups of rows "Kawasaki" and if more rows are added to the bikes that divided the main object further into equal parts and update
const mainObject =[
{ name:"john", age:"23"},
{ name:"Max", age:"26"},
{ name:"Tim", age:"28"},
{ name:"Jim", age:"28"},
{ name:"Jacob", age:"28"},
{ name:"Luke", age:"28"}
]
The expected Outcome should be
const mainObject =[
{ name:"john", age:"23",bike:"honda"},
{ name:"Max", age:"26",bike:"honda"},
{ name:"Tim", age:"28",bike:"kawasaki"},
{ name:"Jim", age:"28",bike:"kawasaki"},
{ name:"Jacob", age:"28",bike:"suzuki"},
{ name:"Luke", age:"28", bike:"suzuki"}
]
Something like this should do the trick:
result = mainObject.map((o, i) => ({
...o,
bike: bikes[Math.floor(i * bikes.length / mainObject.length)]
}))
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
Suppose I have following array
array = [
{
optionA: "QA1",
optionB: "QB1",
optionC: "QC1",
optionD: "QD1",
},
{
optionA: "QA2",
optionB: "QB2",
optionC: "QC2",
optionD: "QD2",
optionE: "QD2",
},
{
optionA: "QA3",
optionB: "QB3",
}
]
Expected output
tableHeader = {OptionA,OptionB,OptionC,OptionD,OptionE}
Wanted display questions data in a table format, which is not organised and has n number for options.
You can get all keys as follows
const array = [{optionA: "QA1",optionB: "QB1",optionC: "QC1",optionD: "QD1",},{optionA: "QA2",optionB: "QB2",optionC: "QC2",optionD: "QD2",optionE: "QD2",},{optionA: "QA3",optionB: "QB3",}]
const allKeys = Object.keys(Object.assign({}, ...array))
console.log(allKeys)
Try this one
let header = new Set();
array.forEach((a) => {
Object.keys(a).forEach((item) => {
header.add(item);
});
});
console.log(header);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Hello so basically I wonder how I can achieve to select property from object with:
objectName.someVariable.
For example how to make this code console.outing number from object:
var a = function(selected){
console.log(selected);
var q = {
black: 1,
red: 2,
blue: 3
}
console.log(q.selected);
console.log(q+'.'+selected);
}
You can access to an object property with:
a point like this: q.black
with two [] like this : q.["black"] or q.[aVariable] //aVariable = "black"
In your example, you can simply do like this:
var a = function(selected){
console.log(selected);
var q = {
black: 1,
red: 2,
blue: 3
}
console.log(q.selected); // Serve to nothing.
console.log(q[selected]+'.'+selected);
}
You can consult w3schools if you want more details.
Tell me if you have some questions.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am new to Javascript and jquery.I want to arrange the date from items[] array to item1[] and item2[] array in ascending and descending order.
var items = ["01-Jun-17", "03-Jun-17", "03-May-17", "05-Jun-17", "05-Jun-17", "18-May-17"];
Output:
/*----logic for Ascending:::------ *\
var item1=["03-May-17","18-May-17","01-Jun-17","03-Jun-17","05-Jun-17","05-Jun-17"];
/*----- logic for Descending:::------ *\
var item2=["05-Jun-17","05-Jun-17","03-Jun-17","01-Jun-17","18-May-17","03-May-17"];
If you are asking about sorting the array, then you could convert the strings to Date, and then apply sort function on it.
var items = ["01-Jun-17", "03-Jun-17", "03-May-17", "05-Jun-17", "05-Jun-17", "18-May-17"];
items.sort((d1,d2) => new Date(d1) - new Date(d2) > 0);
console.log("Ascending: ",items);
console.log("Descending: ",items.reverse());
Here I have added both ascending and descending. You can use javascript array's function sort.
var items = ["01-Jun-17", "03-Jun-17", "03-May-17", "05-Jun-17", "05-Jun-17", "18-May-17"];
var items1 =jQuery.makeArray(items).sort(function(a, b) {
return new Date(a) < new Date(b);
});
var items2 = jQuery.makeArray(items).sort(function(a, b) {
return new Date(a) > new Date(b);
});
$("#div0").html(items);
$("#div1").html(items1);
$("#div2").html(items2);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<lable>Original</lable>
<div id="div0"></div>
</br>
<lable>Asc</lable>
<div id="div1"></div>
</br>
<lable>Desc</lable>
<div id="div2"></div>