Home > front end >  How Can I Make a conditional with return in a .map without get out of main function?
How Can I Make a conditional with return in a .map without get out of main function?

Time:03-09

My objetive is create this object:

{
  name: string.
  birthday: date;
}

by this array:

const dataArray = [
 {
  "id": "name",
  "type": "string";
 },
 {
  "id": "birthday",
  "type": "date";
 }

]

So, I create a .map of the array, like this:

const inputsValues = {};

dataArray.map(item => {
  if(item.type === "date"){
    inputsValues[item.id] = new Date(item.value);
    return; // this line is the problem
  }

  inputsValues[item.id] = item.value;
});

Is there a option to make a return in this function without use else?

CodePudding user response:

const dataArray = [{
    "id": "name",
    "type": "string"
  },
  {
    "id": "birthday",
    "type": "date"
  }

]
console.log(
  dataArray.reduce((acc, curr) => {
    acc[curr.id] = curr.type;
    return acc;
  }, {})
);

CodePudding user response:

Is this what you're trying to achieve?

const dataArray = [
 { "id": "name", "type": "string" },
 { "id": "birthday", "type": "date" }
]

console.log(dataArray.reduce((acc, obj, i) => {
  const key = obj.id;
  const value = obj.type;
  acc[key] = value;
  return acc;
}, {}))

CodePudding user response:

const data = [
  {id: "name", type: "string", value: "John"},
  {id: "birthday", type: "date", value: "02.02.22"},
  {id: "holiday", type: "date", value: "03.03.33"}
]

const inputsValues = data
  // the "return" function - filters all non "date" items
  .filter(item => item.type != "date")
  // the map   inputsValue combination in one function
  .reduce((values, item) => ({[item.id]: item.value, ...values}), {})
  
console.log(inputsValues)

CodePudding user response:

From the question, we miss the item.value definition.

Here is a proposal anyway.

First a conversion function:

const type2obj = (t) => {   
  switch(t) {
    case 'date': return new Date();
    case 'string': return 'thestringvalue';
    default: return 'unknown type';
  }
}

then

  const inputsValues = Object.values(dataArray).
    reduce( (p, {id, type}) => ({...p, [id]:type2obj2(type)}), {})

explanation : we loop through the values of your dataArray and for each of them we reduce starting from the empty object (at the end) and each time adding [id]:type2obj2(type) that is a property whose name is the id and whose value depends on the function defined above.

If you don't like reduce, you can adapt the idea of type2obj to your own code.

In the browser console this gives :

{ name: "thestringvalue", 
  birthday: Date Wed Mar 09 2022 00:19:39 GMT 0100 (Central European Standard Time) }

CodePudding user response:

Using map only to loop on an array, without using the returned array, is an anti pattern. The function map creates a new array by applying the given callback to all the values of an array ie dataArray. This is not your case because you want an object at the end, not an array.

Tu build your structure you should use, instead, a for of loop, a forEach or Array.reduce().

In case you just need to reduce your array to one single object, reduce is the best option:

const dataArray = [
    {
        "id": "name",
        "type": "string"
    },
    {
        "id": "birthday",
        "type": "date"
    }
];

const obj = dataArray.reduce((carry, { id, type, value = '' }) => {
    carry[id] = type === 'date' ? new Date() : value;

    return carry;
}, {}); // remember to initialize your carry, see reduce doc.

console.log(obj); // My reduced object { name: '', date: 2022-...}
  • Related