["10","13"] is the array, need to find the values between them and flatten the array with the values -> ["10","11","12","13"].
The first array above (["10", "13"]) is in a list of arrays.
const OriginalData = {
Red:{
Name:"L",
List:[
["1", "5"],
["2", "5"],
["7", "9" ],
]
},
Blue:{
Name:"BL",
List:[
["1", "5"],
["7", "9" ],
["10", "13" ],
["15", "20"]
]
},
Black:{
List:[
["Random"],
"Random2"
]
}
}
Then finally Object must look like,
{
Level:{
Name:"L",
List:[
1, 2, 3, 4, 5, 7, 8, 9
]
},
Basement:{
Name:"BL",
List:[
1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19, 20
]
},
Custom:{
List:[
"Random",
"Random2"
]
}
}
What It should do: Take the first object, inside List there are set of ranges, the values between those ranges should be found a flatten without duplicates. Finding the values between is only for "Red", and "Blue", In "Black" key only flatten is needed.
I tried,
Code:
const submitData = () => {
let obj = originalData;
let flattenedArray = [].concat.apply([], originalData.Red.List);
let uniqueArray = flattenedArray.filter(
(v, i, a) => a.indexOf(v) === i
);
obj = {
...originalData,
Red: {
...originalData.Red,
List: uniqueArray,
},
};
console.log(obj);
};
The above code flattens the array but will not find between the numbers and it only worked for key "Red"
CodePudding user response:
You can flat the array, get the min and max and finally with a loop create the desired array.
const array = [["1","5"],["7","9"],["10","13"],["15","20"]];
const flatted = array.flat();
const min = Math.min(...flatted);
const max = Math.max(...flatted);
const result = Array.from({length: max 1 - min}).map(function(_, i) {
return i min;
});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
CodePudding user response:
A simple example to create a range:
let example = ["10","13"];
let min = Math.min(...example);
let max = Math.max(...example);
let result = [];
for (i = min; i <= max; i ) {
result.push(i);
}
console.log(min, max, result)
CodePudding user response:
You can easily achieve it with a simple logic and will work for random numbers as well.
Try this (Descriptive comments of implementation has been added in the below code snippet) :
const OriginalData = {
Red:{
Name:"L",
List:[
["1", "5"],
["2", "5"],
["7", "9" ],
]
},
Blue:{
Name:"BL",
List:[
["1", "5"],
["7", "9" ],
["10", "13" ],
["15", "20"]
]
}
};
Object.keys(OriginalData).forEach(key => {
// Flatten the original array list.
OriginalData[key].List = OriginalData[key].List.flat()
// Find min and max numbers from the array.
const min = Math.min(...OriginalData[key].List);
const max = Math.max(...OriginalData[key].List);
// empty existing list array.
OriginalData[key].List = [];
// Now using for loop assign the values to the list array based on min and max value.
for (let i = min; i <= max; i ) {
OriginalData[key].List.push(i);
}
});
// Result
console.log(OriginalData);
CodePudding user response:
Hope the below codes help you.
const OriginalData = {
Red: {
Name: "L",
List: [
["1", "5"],
["2", "5"],
["7", "9"],
]
},
Blue: {
Name: "BL",
List: [
["1", "5"],
["7", "9"],
["10", "13"],
["15", "20"]
]
},
Black: {
List: [
["Random"],
"Random2"
]
}
};
Object.keys(OriginalData).forEach(key => {
OriginalData[key].List = OriginalData[key].List.flat();
if (!isNaN(parseInt(OriginalData[key].List[0]))) {
const min = Math.min(...OriginalData[key].List);
const max = Math.max(...OriginalData[key].List);
let tmpArr = [];
for (let i = min; i <= max; i ) {
tmpArr.push(i);
}
OriginalData[key].List = tmpArr;
}
});
console.log(OriginalData);