1

I have an array like this in javascript,

data = [
    {
        item: "Banana",
        path: "fruit"
    },
    {
        message: "Volvo",
        path: "car"
    },
    {
        message: "Cat",
        path: "animal"
    }
]

And I need to create new object like this,

data = {
    fruit: "Banana",
    car: "Volvo",
    animal: "Cat"
}

I've try this loop,

var newData = [];

data.map(value => {
    newData[value.path] = value.message
});

But this loop keep return like this,

data = [
    {
        fruit: "Banana"
    },
    {
        car: "Volvo"
    },
    {
        animal: "Cat"
    }
]

How to solve case like this? Thank you

0

5 Answers 5

4

You could use .reduce

const data = [
  {
    item: 'Banana',
    path: 'fruit'
  },
  {
    message: 'Volvo',
    path: 'car'
  },
  {
    message: 'Cat',
    path: 'animal'
  }
]

const res = data.reduce((acc, el) => ({ ...acc, [el.path]: el.message || el.item }), {})

console.log(res)

Or continue with your solution, but change array [] to object {} instead

const data = [
  {
    item: 'Banana',
    path: 'fruit'
  },
  {
    message: 'Volvo',
    path: 'car'
  },
  {
    message: 'Cat',
    path: 'animal'
  }
]

var newData = {}

data.map(value => {
  newData[value.path] = value.message || value.item
})

console.log(newData)

1
  • Thank you so much, I didn't realize I forgot to change the type of initial new data. Good job!
    – Schreiner
    Sep 17, 2020 at 17:17
3

Try using Array.prototype.reduce instead of Array.prototype.map :

const data = [{ message: "Banana", path: "fruit" }, { message: "Volvo", path: "car"}, { message: "Cat", path: "animal" }];

const merge = (data) => {
  return data.reduce((acc, o) => {
    acc[o.path] = o.message;
    return acc;
  }, {});
}
console.log(merge(data));

2

You could build an object with the entries.

const
    data = [{ item: "Banana", path: "fruit" }, { message: "Volvo", path: "car" }, { message: "Cat", path: "animal" }],
    object = Object.fromEntries(data.map(({ item, message, path }) =>
        [path, item || message]
    ));

console.log(object);

1

Inside the .map callback, return a new object (not just a property assignment), where the object's key is the path key in the object, and the value is the value of the first non-path key:

const data = [
    {
        item: "Banana",
        path: "fruit"
    },
    {
        message: "Volvo",
        path: "car"
    },
    {
        message: "Cat",
        path: "animal"
    }
];

const newData = data.map(({ path, ...rest }) => ({
  [path]: Object.values(rest)[0]
}));
console.log(newData);

1

Your solution is almost perfect! It is a good approach to use the square brackets notation for this cases as you are doing.

You can achieve the result you are looking by filling the properties of an empty object instead of mapping the values. Like this:

var newData = {};

data.forEach(value => {
    newData[value.path] = value.message;
});

That is because your expected result is not an array (var newData = []), but an object (var newData = {}).

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.