0

I am trying to create an array where the items/Definition of the array are depended on another array. Like I have an dynamic array

arrayDef =[ { title:'item1',validtors:'..',....}
          , { title:'item2',validtors:'..',....}
          ,..]

Now I need to create an array using title from the dynamic array. Later I need to append to same array for new row. Example,

arrayData=[ {item1:'',item2:'',...} ]

arrayDef is being created by user dynamically where they can add column headers and validations. arrayData is to be created using that. I have tried few methods like map, but is looking into most optimal and time saving approach.

Thankyou.

2 Answers 2

2

You could use reduce and wrap result of reduce in an array.

const arrayDef = [
  { title: "item1", validtors: ".." },
  { title: "item2", validtors: ".." },
];

let arrayData = [
  arrayDef.reduce((acc, curr) => {
    const { title } = curr;
    acc[title] = "";
    return acc;
  }, {}),
];

console.log(arrayData);

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

Comments

1

I believe the best approach is to use something like reduce.

arrayDef.reduce((acc, item) => {
    acc[item.title] = '';
    return acc;
}, {})

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.