0

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

3
  • model of first OBJECT interface Luck { testTargetName: string; in: Inumber;low:number;medium: number; } Commented Jul 20, 2021 at 3:54
  • Have you tried using the map fuction already? Commented Jul 20, 2021 at 4:21
  • And you could use the flat fuction to flatten your jagged array Commented Jul 20, 2021 at 4:25

4 Answers 4

1

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)

Sign up to request clarification or add additional context in comments.

Comments

0
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

0

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.