1

Is it possible to flatten nested array object to single object.In my query i want to remove the source object and make the resultant object as one object (I have mentioned my output also).

    var result = [
    {"_id":"12345",
    "_type":"feeds",
    "_source":{
              "title": "hi all solve it",
              "link": "www.face.com",
              "content": "Hi thewwewewedwe asdasdasdasd",
              "createdAt": "2018-08-08T11:42:40.073Z",
              "updatedAt": "2018-08-08T11:42:40.073Z",
              "reply": []
                }
    }]

 //resultant array

     var newResult = [
        {
            "_id":"12345",
            "_type":"feeds",
            "title": "hi all solve it",
            "link": "www.face.com",
            "content": "Hi thewwewewedwe asdasdasdasd",
            "createdAt": "2018-08-08T11:42:40.073Z",
            "updatedAt": "2018-08-08T11:42:40.073Z",
            "reply": []
        }];
deepak masakale
  • 105
  • 1
  • 8

3 Answers3

1

You could use ...spread for that

var result = [{
  "_id":"12345",
  "_type":"feeds",
  "_source": {
    "title": "hi all solve it",
    "link": "www.face.com",
    "content": "Hi thewwewewedwe asdasdasdasd",
    "createdAt": "2018-08-08T11:42:40.073Z",
    "updatedAt": "2018-08-08T11:42:40.073Z",
    "reply": []
  }
}];
    

const { _source, ...rest } = result[0];

const flattenResult = [{
  ...rest,
  ..._source,
}];

console.log(flattenResult);

Leaving the solution for result.length > 1 to you as an exercise.

Lyubomir
  • 19,615
  • 6
  • 55
  • 69
0

You can first loop through the array to get each of the object in the array and then loop through the object keys to get the key names. Then if you encounter the key with name _source then assign those object content into the flatten object using Object.assign(). This will work for result array with one or more than one object.

var result = [{
  "_id": "12345",
  "_type": "feeds",
  "_source": {
    "title": "hi all solve it",
    "link": "www.face.com",
    "content": "Hi thewwewewedwe asdasdasdasd",
    "createdAt": "2018-08-08T11:42:40.073Z",
    "updatedAt": "2018-08-08T11:42:40.073Z",
    "reply": []
   }
},
{
  "_id": "1234567",
  "_type": "feeds123",
  "_source": {
    "title": "hi all solve it 123",
    "link": "www.face.com",
    "content": "Hi thewwewewedwe asdasdasdasd",
    "createdAt": "2018-08-08T11:42:40.073Z",
    "updatedAt": "2018-08-08T11:42:40.073Z",
    "reply": []
  }
}];
var newArray = [];
result.forEach(function(obj){
  var tempObj = {};
  Object.keys(obj).forEach(function(key){
    if(key !== '_source'){
      tempObj[key] = obj[key];
    } else {
      tempObj = Object.assign(tempObj,  obj[key]);
    }
  });
  newArray.push(tempObj);
});
console.log(newArray);
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
0

Simplest version using plain JS

Handles more than one entry

var result = [{ "_id": "12345", "_type": "feeds", "_source": { "title": "hi all solve it", "link": "www.face.com", "content": "Hi thewwewewedwe asdasdasdasd", "createdAt": "2018-08-08T11:42:40.073Z", "updatedAt": "2018-08-08T11:42:40.073Z", "reply": [] } },{ "_id": "12346", "_type": "feeds", "_source": { "title": "hi all solve it", "link": "www.face.com", "content": "Hi thewwewewedwe asdasdasdasd", "createdAt": "2018-08-08T11:42:40.073Z", "updatedAt": "2018-08-08T11:42:40.073Z", "reply": [] } }]

result = result.map(function(item) {
  var obj = item._source;
  for (var o in item) {
    if (o != "_source") obj[o] = item[o];
  }
  return obj;
})
console.log(result)
mplungjan
  • 169,008
  • 28
  • 173
  • 236