1

I got an object which contains some values.

How do i transform this properly?

I tried multiple things like includes(), find(), some(), for-loops, but it seems like the check if 'conversationWith' is included in the object is failing.

The object looks like this:


    {
  _id: 5d3eed4b8558ab0fc513a3b5,
  subject: 'Subject',
  message: 'Message',
  newMessage: true,
  from: 5d3b0585181c521610a15241,
  fromName: 'John Doe',
  to: 5d3b0749c9b633171fa62a48,
  toName: 'Leeroy Jenkins',
  conversationWith: 'Leeroy Jenkins'
}
{
  _id: 5d3eed608558ab0fc513a3b7,
  subject: '2nd',
  message: '2nd',
  newMessage: true,
  from: 5d3b0585181c521610a15241,
  fromName: 'John Doe',
  to: 5d3b0749c9b633171fa62a48,
  toName: 'Leeroy Jenkins',
  conversationWith: 'Leeroy Jenkins'
}
{
  _id: 5d3ef15e6a570c1201457918,
  subject: '3rd',
  message: '3rd',
  newMessage: true,
  from: 5d3b0585181c521610a15241,
  fromName: 'John Doe',
  to: 5d3b0749c9b633171fa62a48,
  toName: 'Leeroy Jenkins',
  conversationWith: 'Leeroy Jenkins'
}

I want to create an array like this out of it:


    [{ 
       conversationWith: "Leeroy Jenkins",
       message: ["Message", "2nd", "3rd"]
    }]

The Problem is that i have multiple objects that I'm looping through, where I only want the 'conversationWith' property to be inserted into the array once, while i want all the messages in it.

I either get the 'conversationWith' property inserted multiple times or not at all.

8
  • Can you provide a bigger array, rather than one object? And give an example of an input and expected output? Commented Jul 29, 2019 at 13:18
  • It looks like you want -> developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jul 29, 2019 at 13:20
  • I assume that all messages come from 'John Doe' and that you do not have to filter them anymore? And that the output should be an object instead of an objects wrapped inside an array? Else the question doesn't not make sense to me and I would request more information about the expected output. Commented Jul 29, 2019 at 13:21
  • If you only store conversationWith inside the newly created objects, then you will have to loop through them all the time to figure out if you got one with the current conversationWith already. I’d recommend to use conversationWith as a key to begin with, that makes these “look ups” easier. Commented Jul 29, 2019 at 13:22
  • Shouldn't you first filter the messages by conversationWith and toName? Then you could grab all of the messages from the filtered results? Commented Jul 29, 2019 at 13:24

2 Answers 2

2

You can use a simple forEach loop

var a= [{
  _id: '5d3eed4b8558ab0fc513a3b5',
  subject: 'Subject',
  message: 'Message',
  newMessage: true,
  from: '5d3b0585181c521610a15241',
  fromName: 'John Doe',
  to: '5d3b0749c9b633171fa62a48',
  toName: 'Leeroy Jenkins',
  conversationWith: 'Leeroy Jenkins'
},
{
  _id: '5d3eed608558ab0fc513a3b7',
  subject: '2nd',
  message: '2nd',
  newMessage: true,
  from: '5d3b0585181c521610a15241',
  fromName: 'John Doe',
  to: '5d3b0749c9b633171fa62a48',
  toName: 'Leeroy Jenkins',
  conversationWith: 'Leeroy Jenkins'
},
{
  _id: '5d3ef15e6a570c1201457918',
  subject: '3rd',
  message: '3rd',
  newMessage: true,
  from: '5d3b0585181c521610a15241',
  fromName: 'John Doe',
  to: '5d3b0749c9b633171fa62a48',
  toName: 'Leeroy Jenkins',
  conversationWith: 'Leeroy Jenkins'
}]
    var b=[];
    var bool=false;
    a.forEach(function(e){
        b.forEach(function(k){
            if(k.conversationWith==e.conversationWith)
            {
                bool=true;
                k.message.push(e.message)
            }
        })
        if(bool==false)
        {
            var obj={};
            obj.conversationWith=e.conversationWith;
            obj.message=[];
            obj.message.push(e.message);
            b.push(obj);
        }
        bool=false;
    })
    console.log(b)

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

2 Comments

The problem is that i want 'conversationWith' only added once to the array.
This works! Thank you very very much for taking your time!
0

Array reduce approach

The reduce() method executes a reducer function (that you provide) on each element of the array, resulting in a single output value.

use Array.prototype.reduce to return an array of new objects created from source array with Array.prototype.some and Array.prototype.filter and Array.prototype.map and Array.prototype.join to fitler and join the messages together

https://repl.it/repls/TealVitalDatum

const LEROY = 'Leeroy Jenkins';
const convoWithMessage = [{
      _id: '5d3eed4b8558ab0fc513a3b4',
      subject: 'Subject',
      message: 'Message',
      newMessage: true,
      from: '5d3b0585181c521610a15241',
      fromName: 'John Doe',
      to: '5d3b0749c9b633171fa62a48',
      toName: 'Leeroy Jenkins',
      conversationWith: 'John Doe'
    }, {
      _id: '5d3eed4b8558ab0fc513a3b4',
      subject: 'Subject',
      message: 'Message',
      newMessage: true,
      from: '5d3b0585181c521610a15241',
      fromName: 'John Doe',
      to: '5d3b0749c9b633171fa62a48',
      toName: 'Leeroy Jenkins',
      conversationWith: 'John Doe'
    }].reduce((acc, item, index, array) => {
        const isLeroy = d => d.toName === LEROY;
        const leroys = acc.filter(isLeroy);
        if (item.toName === LEROY) {
            if (array.some(isLeroy)) {
                const message = [item.message, ...leroys.map(i => i.message)];
          return [...leroys.filter(isLeroy), { name: LEROY, message }]
            }
        }
    return [...acc, { toName: item.toName, message: item.message }];
    }, [])

    console.log(convoWithMessage)

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.