I have array Object
[
{ testTargetname: "Test system", in: 2, low: 1, medium: 2 },
{ testTargetname: "Test app", in: 2, low: 1, medium: 0 }]
I don't know how to convert to the new array object below. Please help me
[
{ in: 1, testTargetname: "Test system" },
{ in: 1, testTargetname: "Test system" },
{ low: 1, testTargetname: "Test system" },
{ medium: 1, testTargetname: "Test system" },
{ medium: 1, testTargetname: "Test system" },
{ medium: 1, testTargetname: "Test system" },
{ in: 1, testTargetname: "Test app" },
{ in: 1, testTargetname: "Test app" },
{ low: 1, testTargetname: "Test app" },
]
When medium = 0, It's not displayed in new array
-
model of first OBJECT interface Luck { testTargetName: string; in: Inumber;low:number;medium: number; }Dr. Rill– Dr. Rill2021年07月20日 03:54:40 +00:00Commented Jul 20, 2021 at 3:54
-
Have you tried using the map fuction already?Pieterjan– Pieterjan2021年07月20日 04:21:00 +00:00Commented Jul 20, 2021 at 4:21
-
And you could use the flat fuction to flatten your jagged arrayPieterjan– Pieterjan2021年07月20日 04:25:00 +00:00Commented Jul 20, 2021 at 4:25
4 Answers 4
You can use reduce and inside the callback iterate the current object and check if the value of the current key is number and greater than 0
let data = [{
testTargetname: "Test system",
in: 2,
low: 1,
medium: 2
},
{
testTargetname: "Test app",
in: 2,
low: 1,
medium: 0
}
];
var val = data.reduce(function(d, a) {
for (const b in a) {
if (!isNaN(a[b]) && 0 < a[b]) {
for (var e = 0; e < a[b]; e++) {
d.push({testTargetname : a.testTargetname, [b] : 1});
}
}
}
return d;
}, []);
console.log(val)
Comments
const array = [
{ testTargetname: "Test system", in: 2, low: 1, medium: 2 },
{ testTargetname: "Test app", in: 2, low: 1, medium: 0 }]
let createObject = (_object, _key) => {
const newKeyArray = []
for(let i = 0; i < _object[_key] || 0; i++){
let newObject = {}
newObject[_key] = 1//_object[_key]
newObject["testTargetname"] = _object["testTargetname"]
newKeyArray.push(newObject)
}
return newKeyArray;
}
newArray = [];
array.forEach( (_object) => {
["in", "low", "medium"].forEach((_key) => {
newArray.push( ...createObject(_object, _key) )
})
})
console.log(newArray)
Comments
This is more of a JavaScript/TypeScript question as it doesn't really have anything to do with Angular.
const source = [
{ testTargetname: "Test system", in: 2, low: 1, medium: 2 },
{ testTargetname: "Test app", in: 2, low: 1, medium: 0 },
];
source.reduce((result, item) => {
const keys = Object.keys(item).filter((key) => key !== "testTargetname");
const itemsToAdd = keys.reduce((response, key) => {
let addToResponse: Record<string, string | number>[];
for (let i = 1; i <= item[key]; i++) {
[...addToResponse, { [key]: 1, testTargetname: item.testTargetname }];
}
return !!addToResponse ? [...response, ...addToResponse] : response;
}, [] as Record<string, string | number>[]);
return [...result, ...itemsToAdd];
}, [] as Record<string, string | number>[]);
Comments
I see nobody tried flatMap, so please have a look at my solution
const list = [
{
testTargetname: 'Test system',
in: 2,
low: 1,
medium: 2
},
{
testTargetname: 'Test app',
in: 2,
low: 1,
medium: 0
}
]
const newList = list.flatMap((currentItem) => {
const targetKey = 'testTargetname'
return Object.entries(currentItem)
.filter(([key]) => key !== targetKey)
.map((item) => {
const obj = Object.fromEntries([item])
return {
...obj,
[targetKey]: currentItem[targetKey]
}
})
})
console.log(newList)
PS, it is an example of how to map it to the proper structure, but if you want to filter some particular values then at the end just use the filter function
Comments
Explore related questions
See similar questions with these tags.