I want to filter from two different arrays based on following array named = timeArray
["13:37", "13:36", "13:35", "13:34", "13:33", "13:32"]
first array looks like this .. array1
[
[23323.25,23323.65,23313.25,23315.05,97,62,"13:36"],
[23315.05,23315.2,23314,23315,8,9,"13:37"]
]
second array looks like this .. array2
[
[23310,23310,23300,23300,0,0,"13:34"],
[23309.75,23343.1,23305,23323.25,0,0,"13:35"],
[23296.5,23310,23294.65,23309.8,0,0,"13:30"],
[23308.35,23310,23301,23306.15,0,0,"13:31"],
[23308,23309,23292.5,23299.55,0,0,"13:32"],
[23299.55,23310,23294.15,23310,0,0,"13:33"],
[23310,23310,23300,23300,0,0,"13:34"],
[23309.75,23343.1,23305,23324.65,0,0,"13:35"],
[23308,23309,23292.5,23299.55,0,0,"13:36"]
]
Based on Timearray elements, First should check array1 and then array 2 6th element and expected result should look like
[
[23308,23309,23292.5,23299.55,0,0,"13:32"],
[23299.55,23310,23294.15,23310,0,0,"13:33"],
[23310,23310,23300,23300,0,0,"13:34"],
[23309.75,23343.1,23305,23324.65,0,0,"13:35"],
[23323.25,23323.65,23313.25,23315.05,97,62,"13:36"],
[23315.05,23315.2,23314,23315,8,9,"13:37"]
]
I tried something like below
var finalarray = [];
for(var key in timeArray)
{
var timer = timeArray[key];
for(var key in array1)
{
arraytime1 = array1[key][6];
if(timer == arraytime1)
{
finalarray.push(arraytime1);
}
}
for(var key in array2)
{
arraytime2 = array2[key][6];
if(timer == arraytime2)
{
finalarray.push(arraytime2);
}
}
}
But "13:36" is added two times...Also if the element based on timerArray is not present it should be null... also is there any better way to do it ? I feel like it takes much resource to process... Thank you.
You could take an object for keeping the fist array for each time and map the result.
const
timeArray = ["13:37", "13:36", "13:35", "13:34", "13:33", "13:32"],
array1 = [[23323.25, 23323.65, 23313.25, 23315.05, 97, 62, "13:36"], [23315.05, 23315.2, 23314, 23315, 8, 9, "13:37"]],
array2 = [[23310, 23310, 23300, 23300, 0, 0, "13:34"], [23309.75, 23343.1, 23305, 23323.25, 0, 0, "13:35"], [23296.5, 23310, 23294.65, 23309.8, 0, 0, "13:30"], [23308.35, 23310, 23301, 23306.15, 0, 0, "13:31"], [23308, 23309, 23292.5, 23299.55, 0, 0, "13:32"], [23299.55, 23310, 23294.15, 23310, 0, 0, "13:33"], [23310, 23310, 23300, 23300, 0, 0, "13:34"], [23309.75, 23343.1, 23305, 23324.65, 0, 0, "13:35"], [23308, 23309, 23292.5, 23299.55, 0, 0, "13:36"]],
times = [array1, array2].reduce((r, array) => {
array.forEach(a => r[a[6]] ??= a)
return r;
}, {}),
result = timeArray
.sort()
.map(time => times[time] || [0, 0, 0, 0, 0, time]);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I believe this is what you want
const base = ["13:37", "13:36", "13:35", "13:34", "13:33", "13:32"]
const arrayA = [
[23323.25,23323.65,23313.25,23315.05,97,62,"13:36"],
[23315.05,23315.2,23314,23315,8,9,"13:37"]
]
const arrayB = [
[23310,23310,23300,23300,0,0,"13:34"],
[23309.75,23343.1,23305,23323.25,0,0,"13:35"],
[23296.5,23310,23294.65,23309.8,0,0,"13:30"],
[23308.35,23310,23301,23306.15,0,0,"13:31"],
[23308,23309,23292.5,23299.55,0,0,"13:32"],
[23299.55,23310,23294.15,23310,0,0,"13:33"],
[23310,23310,23300,23300,0,0,"13:34"],
[23309.75,23343.1,23305,23324.65,0,0,"13:35"],
[23308,23309,23292.5,23299.55,0,0,"13:36"]
]
// const expectedResult = [
// [23308,23309,23292.5,23299.55,0,0,"13:32"],
// [23299.55,23310,23294.15,23310,0,0,"13:33"],
// [23310,23310,23300,23300,0,0,"13:34"],
// [23309.75,23343.1,23305,23324.65,0,0,"13:35"],
// [23323.25,23323.65,23313.25,23315.05,97,62,"13:36"],
// [23315.05,23315.2,23314,23315,8,9,"13:37"]
//]
const res = base.sort().map(time => {
return arrayA.find(subArray => subArray[subArray.length - 1] === time)
|| arrayB.find(subArray => subArray[subArray.length - 1] === time)
|| null
});
console.log(res);
I would modify the arrays in the following format to obtain a much faster access:
{
"13:34": [23310,23310,23300,23300,0,0]
}
Then find the appropriate array for each time item.
Code:
const timeArray = ["13:37", "13:36", "13:35", "13:34", "13:33", "13:32"]
const array1 = [
[23323.25,23323.65,23313.25,23315.05,97,62,"13:36"],
[23315.05,23315.2,23314,23315,8,9,"13:37"]
]
const array2 = [
[23310,23310,23300,23300,0,0,"13:34"],
[23309.75,23343.1,23305,23323.25,0,0,"13:35"],
[23296.5,23310,23294.65,23309.8,0,0,"13:30"],
[23308.35,23310,23301,23306.15,0,0,"13:31"],
[23308,23309,23292.5,23299.55,0,0,"13:32"],
[23299.55,23310,23294.15,23310,0,0,"13:33"],
[23310,23310,23300,23300,0,0,"13:34"],
[23309.75,23343.1,23305,23324.65,0,0,"13:35"],
[23308,23309,23292.5,23299.55,0,0,"13:36"]
]
const values1 = {}
array1.forEach(item => {
values1[item[6]] = item
})
const values2 = {}
array2.forEach(item => {
values2[item[6]] = item
})
const result = []
timeArray.sort().forEach(time => {
let itemResult = [0,0,0,0,0,0, time]
if (values1[time]) {
itemResult = values1[time]
} else if (values2[time]) {
itemResult = values2[time]
}
result.push(itemResult)
})
console.log(result)
in my opinion you can do like this
var finalarray = [];
for(var key in timeArray)
{
var timer = timeArray[key];
for(var key in array2)
{
arraytime2 = array2[key][6];
if(timer == arraytime2)
{
finalarray[arraytime2]=array2[key];
}
}
for(var key in array1)
{
arraytime1 = array1[key][6];
if(timer == arraytime1)
{
finalarray[arraytime1]=array1[key];
}
}
}
It is possible to use Map collection to have O(1) while mapping the items. So we can sort timeArray and then just map() items:
const unique_1 = new Map(array1.map(s => [s[6], s]));
const unique_2 = new Map(array2.map(s => [s[6], s]));
const result = timeArray
.sort()
.map(time => unique_1.get(time) || unique_2.get(time));
An example:
const timeArray = ["13:37", "13:36", "13:35", "13:34", "13:33", "13:32"]
const array1 = [
[23323.25, 23323.65, 23313.25, 23315.05, 97, 62, "13:36"],
[23315.05, 23315.2, 23314, 23315, 8, 9, "13:37"]
]
const array2 = [
[23310, 23310, 23300, 23300, 0, 0, "13:34"],
[23309.75, 23343.1, 23305, 23323.25, 0, 0, "13:35"],
[23296.5, 23310, 23294.65, 23309.8, 0, 0, "13:30"],
[23308.35, 23310, 23301, 23306.15, 0, 0, "13:31"],
[23308, 23309, 23292.5, 23299.55, 0, 0, "13:32"],
[23299.55, 23310, 23294.15, 23310, 0, 0, "13:33"],
[23310, 23310, 23300, 23300, 0, 0, "13:34"],
[23309.75, 23343.1, 23305, 23324.65, 0, 0, "13:35"],
[23308, 23309, 23292.5, 23299.55, 0, 0, "13:36"]
]
const unique_1 = new Map(array1.map(s => [s[6], s]));
const unique_2 = new Map(array2.map(s => [s[6], s]));
const result = timeArray
.sort()
.map(time => unique_1.get(time) || unique_2.get(time));
console.log(result)
const obj = {
"pi_diagram": null,
"painting": null,
"heat_treatment": null,
"welding_procedure": null,
"inspection_test": null,
"pipecl_hadoop": null,
"pipecl": null,
"ludo_min_hadoop": null,
"ludo_min": 4,
"ludo_normal_hadoop": null,
"ludo_normal": 6,
"ludo_max_hadoop": null,
"ludo_max": null,
"ludo_test": null,
"ludo_mech_min": null,
"ludo_mech_max": null,
"ludo_unit": "barg",
"temp_min_hadoop": null
}
I am having this object , how to extract key value pair having '_hadoop' appended in key ??
you can refer this Q :
JavaScript: filter() for Objects
for your Q it would be as :
const obj = {
"pi_diagram": null,
"painting": null,
"heat_treatment": null,
"welding_procedure": null,
"inspection_test": null,
"pipecl_hadoop": null,
"pipecl": null,
"ludo_min_hadoop": null,
"ludo_min": 4,
"ludo_normal_hadoop": null,
"ludo_normal": 6,
"ludo_max_hadoop": null,
"ludo_max": null,
"ludo_test": null,
"ludo_mech_min": null,
"ludo_mech_max": null,
"ludo_unit": "barg",
"temp_min_hadoop": null
};
const filteredByKey = Object.fromEntries(Object.entries(obj).filter(([key, value]) => key.includes('_hadoop')))
console.log(filteredByKey);
const obj = {
"pi_diagram": null,
"painting": null,
"heat_treatment": null,
"welding_procedure": null,
"inspection_test": null,
"pipecl_hadoop": null,
"pipecl": null,
"ludo_min_hadoop": null,
"ludo_min": 4,
"ludo_normal_hadoop": null,
"ludo_normal": 6,
"ludo_max_hadoop": null,
"ludo_max": null,
"ludo_test": null,
"ludo_mech_min": null,
"ludo_mech_max": null,
"ludo_unit": "barg",
"temp_min_hadoop": null
}
let result = {}
for (const [key, value] of Object.entries(obj)) {
if (key.includes('_hadoop')) {
result[key] = value
}
}
console.log(result)
If you want to strictly check name ends with _hadoop and name doesn't contains _hadoop then you need to use regex like /_hadoop$/.test(propName).
Use Object.keys(obj) to get array of all keys of obj and then filter with /_hadoop$/.test(x) so it will return array of key ends with _hadoop.
Then use reduce to build your new object.
Check the result below.
const obj = {
"pi_hadoop_diagram": null,
"painting": null,
"heat_treatment": null,
"welding_procedure": null,
"inspection_test": null,
"pipecl_hadoop": null,
"pipecl": null,
"ludo_min_hadoop": null,
"ludo_min": 4,
"ludo_normal_hadoop": null,
"ludo_normal": 6,
"ludo_max_hadoop": null,
"ludo_max": null,
"ludo_test": null,
"ludo_mech_min": null,
"ludo_mech_max": null,
"ludo_unit": "barg",
"temp_min_hadoop": null
};
let result = Object.keys(obj)
.filter(x => /_hadoop$/.test(x))
.reduce((a, x) => (a[x] = obj[x], a), {});
console.log(result);
I want to remove all the null values between the first number and the end number.
My code seems far too complicated.
Love to see some more flexible minds at it.
let values = [null, null, null, 1, 2, null, 3, null, 4, null, null, 5, 6, 7, null, null, null, null];
let startIndex = values.findIndex(n => (n !== null))
let endIndex = values.length - 1;
for ( ; endIndex > 0; endIndex--) {
if (values[endIndex] !== null) break;
}
let arrayCopy = values.slice();
for(let i = endIndex; i > startIndex; i--) {
if (values[i] === null) {
arrayCopy.splice(i, 1);
}
}
console.log(arrayCopy)
Quick and dirty, sure it can be done better
var values = [null, null, null, 1, 2, null, 3, null, 4, null, null, 5, 6, 7, null, null, null, null];
var nonNull = values.filter(value => value !== null);
var result = [...values.slice(0, values.indexOf(nonNull[0])), ...nonNull, ...values.slice(values.lastIndexOf(nonNull[nonNull.length - 1]) + 1)];
console.log(result);
I used a filter taking index or null values into account, it is certainly clearer IMO.
If your array is "very large", it might be interesting to find another way to compute the endIndex, but if you don't have performance problems, I think you can keep this part of your code as it is.
let values = [null, null, null, 1, 2, null, 3, null, 4, null, null, 5, 6, 7, null, null, null, null];
let startIndex = values.findIndex(n => (n !== null))
let endIndex = values.length - 1;
for ( ; endIndex > 0; endIndex--) {
if (values[endIndex] !== null) break;
}
let arrayCopy = values.filter((v, i) => i < startIndex || i > endIndex || v !== null);
console.log(arrayCopy)
This approach use reduce function to find the number of nulls in the at the start and at the end of the array, and then to add counted nulls before and after the non null elements.
let values = [null, null, null, 1, 2, null, 3, null, 4, null, null, 5, 6, 7, null, null, null, null];
var reduceFn = function(a,b){
if (!a.finished && b === null){
a.nullCount++;
}
if (b !== null){
a.finished = true;
}
return a;
};
var start = {finished : false, nullCount:0};
values.reduce(reduceFn,start);
var end = {finished : false, nullCount:0};
values.slice().reverse().reduce(reduceFn,end);
result = Array(start.nullCount).fill(null);
result.push.apply(result,values.filter(v => v !== null));
result.push.apply(result,Array(end.nullCount).fill(null));
console.log(result)