3

I want to create a new Array using two arrays.

itemsAll = [
    {id: 1, itemId: 1, serialNo:"11111111", itemTypeId: 1, itemMasterId: 1, itemStatus: 0, updatedBy: 1 },
    {id: 2, itemId: 2, serialNo:"2222222", itemTypeId: 3, itemMasterId: 2, itemStatus: 0, updatedBy: 1 }
];

There is a itemTypeId i want to get itemTypeName match with itemTypeId.

itemType = [
    {id: 3, itemTypeId: 1, itemTypeName: "type Name 1", description: "Hello", itemTypeStatus: 0, status: true },
    {id: 13, itemTypeId: 2, itemTypeName: "type Name 2", description: "222 Hello", itemTypeStatus: 0, status: true },
    {id: 15 , itemTypeId: 3, itemTypeName: "type Name 3", description: "333 Hello", itemTypeStatus: 0, status: true }
];

Expected Array

itemsAllNew = [
    {id: 1, itemId: 1, serialNo:"11111111", itemTypeId: 1, itemTypeName: "type Name 1", itemMasterId: 1, itemStatus: 0, updatedBy: 1 },
    {id: 2, itemId: 2, serialNo:"2222222", itemTypeId: 3, , itemTypeName: "type Name 3", itemMasterId: 2, itemStatus: 0, updatedBy: 1 }
];

I added below tried solution but its contain unwanted key-value pairs also.

const output = itemsAll.map(
    itemsall => Object.assign(itemsall, itemType.find((itemtype) => itemtype.itemTypeId === itemsall.itemTypeId))
);

console.log(output);

Attached screenshot of output.

enter image description here

6
  • Please add the code you've tried Commented Sep 5, 2020 at 13:06
  • Please visit help center, take tour to see what and How to Ask. Do some research, search for related topics on SO; if you get stuck, post a minimal reproducible example of your attempt, noting input and expected output, preferably in a Stacksnippet Commented Sep 5, 2020 at 13:06
  • foreach and developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Sep 5, 2020 at 13:09
  • It is not clear what you really want. You didn’t give us the criteria for associating the two matrices and you also didn’t give us a try Commented Sep 5, 2020 at 13:09
  • 1
    @adiga I added code I tried. Commented Sep 5, 2020 at 13:10

3 Answers 3

2

You can create a Map object and map it with time complexity O(1):

const mapItemType = new Map(itemType.map(i => [i.itemTypeId, i.itemTypeName]));
const result = itemsAll.map(({itemTypeId, ...other}) => 
   ({itemTypeName: mapItemType.get(itemTypeId), ...other }))

An example:

let itemsAll = [
    {id: 1, itemId: 1, serialNo:"11111111", itemTypeId: 1, itemMasterId: 1, itemStatus: 0, updatedBy: 1 },
    {id: 2, itemId: 2, serialNo:"2222222", itemTypeId: 3, itemMasterId: 2, itemStatus: 0, updatedBy: 1 }
];

let itemType = [
    {id: 3, itemTypeId: 1, itemTypeName: "type Name 1", description: "Hello", itemTypeStatus: 0, status: true },
    {id: 13, itemTypeId: 2, itemTypeName: "type Name 2", description: "222 Hello", itemTypeStatus: 0, status: true },
    {id: 15 , itemTypeId: 3, itemTypeName: "type Name 3", description: "333 Hello", itemTypeStatus: 0, status: true }
];

const mapItemType = new Map(itemType.map(i => [i.itemTypeId, i.itemTypeName]));
const result = itemsAll.map(({itemTypeId, ...other}) => ({itemTypeName: mapItemType.get(itemTypeId), ...other }))
console.log(result)

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

Comments

1

You can loop through the itemsAll array and check with the itemTypeId of the item in itemType array.

const itemsAll = [
    {id: 1, itemId: 1, serialNo:"11111111", itemTypeId: 1, itemMasterId: 1, itemStatus: 0, updatedBy: 1 },
    {id: 2, itemId: 2, serialNo:"2222222", itemTypeId: 3, itemMasterId: 2, itemStatus: 0, updatedBy: 1 }
];

const itemType = [
    {id: 3, itemTypeId: 1, itemTypeName: "type Name 1", description: "Hello", itemTypeStatus: 0, status: true },
    {id: 13, itemTypeId: 2, itemTypeName: "type Name 2", description: "222 Hello", itemTypeStatus: 0, status: true },
    {id: 15 , itemTypeId: 3, itemTypeName: "type Name 3", description: "333 Hello", itemTypeStatus: 0, status: true }
];

const findItemTypes = (items, types) => {
  return items.map(item => {
    const { itemTypeName } = (types.find(type => type.itemTypeId === item.itemTypeId) || {});
    return {
    ...item,
    itemTypeName
    }
  })
}

console.log(findItemTypes(itemsAll, itemType))
.as-console-wrapper {
  max-height: 100% !important;
}

1 Comment

There is a unwanted data "description": "Hello" how to exclude them.
1

So you mean you are getting extra key value pairs. e.g. description: '222 Hello' but you don't want that.. So if your solution is working fine then all you need to do is remove the unnecessary fields. One way would be using the map function.

const newArr = arr.map(({Title, ...rest}) => ({...rest})) 

This would remove the Title field and give you the remaining fields. You can do similarly for your use case.

1 Comment

That could be simplified to arr.map(({ Title, ...rest}) => rest)

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.