0

How find properties in an array in object and move these objects to another array in the object? How to extract {"comment": "value"} from this.state.comments and place it in thecomment object in the comments array after other objects? I remapped the array and extracted the values themselves - comment properties. How to extract the whole object so it would look like this {"comment:" value "}

const comment = {
        "comments": [{'comment': 'aaa'}, {'comment': 'bbb'}]
        "description": " ytytyty"
        "id": 3
        "title": "gfgfgfgfgf"
    }

this.state.comments = [
    {"comment": "eeeee"},
    {"comment": "rrrrr"},
    {"comment": "ggggg"},
    {"date: Thu Jun 13 2019 01:27:09 
      "desc": "dfdfdf"
      "comment": "hhhhhh"
    }
]

let v = this.state.comments.map(t => t.comment? t.comment : t);

 console.log(`{comment: ${v}`);

Expected effect:

const comment = {
        "comments": [{'comment': 'aaa'}, {'comment': 'bbb'}, 
           {"comment": "eeeee"}, {"comment": "rrrrr"}, {"comment": 
           "ggggg"}, "comment": "hhhhhh"]
        "description": " ytytyty"
        "id": 3
        "title": "gfgfgfgfgf"
}

3 Answers 3

1

const comment = {
        "comments": [{'comment': 'aaa'}, {'comment': 'bbb'}],
        "description": " ytytyty",
        "id": 3,
        "title": "gfgfgfgfgf"
    }

let newComments = [
    {"comment": "eeeee"},
    {"comment": "rrrrr"},
    {"comment": "ggggg"},
    {"date": "Thu Jun 13 2019 01:27:09", 
      "desc": "dfdfdf",
      "comment": "hhhhhh"
    }
]

newComments.forEach(t => {
  if( t.comment ) comment.comments.push({
    comment: t.comment
  })
});

 console.log(comment);

1
1

const comment = {
        comments: [{comment: 'aaa'}, {comment: 'bbb'}],
        description: " ytytyty",
        id: 3,
        title: "gfgfgfgfgf"
    }

const newComments = [
    {comment: "eeeee"},
    {comment: "rrrrr"},
    {comment: "ggggg"},
    {date: "Thu Jun 13 2019 01:27:09", 
      desc: "dfdfdf",
      comment: "hhhhhh"
    }
];

comment.comments = newComments.reduce((res,obj) => obj.comment ? [...res, {comment : obj.comment}] : res,comment.comments || [])



 console.log(comment);

1
  • I would like to use it in the react, but the times are doubled. ://stackblitz.com/edit/react-jfkwnu
    – Umbro
    Commented Jun 13, 2019 at 8:41
0

Just iterate through each item with forEach and check if the key is comment - if so, push to the comments array.

const comment = {"comments":[{'comment':'aaa'},{'comment':'bbb'}], "description":" ytytyty", "id":3, "title":"gfgfgfgfgf"};
const state = {comments:[{"comment":"eeeee"},{"comment":"rrrrr"},{"comment":"ggggg"},{"date":"Thu Jun 13 2019 01:27:09", "desc":"dfdfdf", "comment":"hhhhhh"}]};
state.comments.forEach(({ comment: c }) => c ? comment.comments.push({ c }) : c);
console.log(comment);
.as-console-wrapper { max-height: 100% !important; top: auto; }

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.