I'm looking for a solution on how to extract only consecutive objects with a null property from an array of objects, please see the example below.
Important note:
Array.proptype.filter((item)=> item.y === null) doesn't solve the problem here, .filter method will simply return an array objects where y is equal null.
What I'm willing to achieve here is actually quite more sophisticated than returning a simple array, the goal is to write a method that can iterate through each item in the array, check if the next item also has a null y, if true, the next item must be added to the extracted subset of objects.
Consecutive items are simply the items that come after each other, if the object with the index 3 has a null y property and the object with index 4 has a null y property as well, objects with indexes 3 and 4 can be considered as consecutive items,
If the item with index 8 has a null y but the items respectively with indexes 7 and 9 doesn't have a null y, only the item with index 8 must be added to the extracted subset.
First use case:
the method must be able to spot consecutive nulls and extract them as a subset of the original input set.
input:
['spider', null, null, null, 'cat','horse', 'eagle']
output:
[
[null, null, null], <= one subset of nulls representing the nulls between 'spider' and 'cat'
]
Second use case:
The method must be able to consider single null items and also extract them into a single item subset array.
input:
['spider', null, null, null, 'cat', null, 'horse', 'eagle']
output:
[
[null, null, null], <== first subset
[null], <== second subset representing the null item between the 'cat' and 'horse'
]
Given the dataSet below, I want to extract the items where y property is null.
output pattern
[
[first-subset-of-consecutive-nulls],
[second-subset-of-consecutive-nulls],
[....]
]
Please note that the expected output data structure is an array of arrays and not an array of objects.
Here's a more real-life dataSet example with the expected output.
The expected output should be an array of arrays that contain a subset of the dataSet where the y property is null and the objects are consecutive.
const dataSet = [
{x: 'foo', y: 'random-y'},
{x: 'bar', y: 'random-y'},
{x: 'yo', y: null}, <=
{x: 'random', y: null}, <=
{x: 'lorem', y: null}, <=
{x: 'ipsum', y: 'random-y'},
{x: 'dolor', y: 'random-y'},
{x: 'var', y: null}, <=
{x: 'mit', y: null}, <=
{x: 'oder', y: 'random-y'},
{x: 'whatever', y: null}, <=
{x: 'something', y: 'random-y'}
];
// Method signature e.g extractSuccesiveNullBy(objectProp: string, from: Array<any>);
extractSuccessiveNullsBy('y', dataSet);
Expected output:
[
[ {x: 'yo', y: null}, {x: 'random', y: null}, {x: 'lorem', y: null}],
[ {x: 'var', y: null}, {x: 'mit', y: null}],
[ {x: 'whatever', y: null]
]
Any proposal will be well appreciated. Thanks.
CodePudding user response:
The hash grouping approach can be applied:
const dataSet = [{x: 'foo', y: 'random-y'},{x: 'bar', y: 'random-y'},{x: 'yo', y: null},{x: 'random', y: null},{x: 'lorem', y: null},{x: 'ipsum', y: 'random-y'},{x: 'dolor', y: 'random-y'},{x: 'var', y: null},{x: 'mit', y: null},{x: 'oder', y: 'random-y'},{x: 'whatever', y: null},{x: 'something', y: 'random-y'}];
let counter = 0;
const result = Object.values(dataSet.reduce((acc, obj) => {
if (obj.y !== null) {
counter = 1;
return acc;
}
acc[counter] = acc[counter] ? [...acc[counter], obj] : [obj];
return acc;
}, {}));
console.log(result);
.as-console-wrapper{min-height: 100%!important; top: 0}
CodePudding user response:
Sorry, misunderstood, you can loop through the array and see if the previous data was null.
function extractSuccessiveNullsBy(array, objectProp) {
let result = [];
let previous = false;
for (const data of array) {
if (data[objectProp] === null) {
if (previous) {
result[result.length - 1].push(data);
} else {
result.push([data]);
}
previous = true;
} else {
previous = false;
}
}
return result;
}
Live Example:
const dataSet = [
{x: 'foo', y: 'random-y'},
{x: 'bar', y: 'random-y'},
{x: 'yo', y: null}, // <=
{x: 'random', y: null}, // <=
{x: 'lorem', y: null}, // <=
{x: 'ipsum', y: 'random-y'},
{x: 'dolor', y: 'random-y'},
{x: 'var', y: null}, // <=
{x: 'mit', y: null}, // <=
{x: 'oder', y: 'random-y'},
{x: 'whatever', y: null}, // <=
{x: 'something', y: 'random-y'}
];
function extractSuccessiveNullsBy(array, objectProp) {
let result = [];
let previous = false;
for (const data of array) {
if (data[objectProp] === null) {
if (previous) {
result[result.length - 1].push(data);
} else {
result.push([data]);
}
previous = true;
} else {
previous = false;
}
}
return result;
}
console.log(extractSuccessiveNullsBy(dataSet, "y"));
.as-console-wrapper {
max-height: 100% !important;
}
