javascript - Select nested array object and replace it - Stack Overflow

I got an array (as result of a mongoDB query) with some elements like this:{ "_id": "Exx

I got an array (as result of a mongoDB query) with some elements like this:

{ 
    "_id": "ExxTDXJSwvRbLdtpg",
    "content": [
        {
            "content": "First paragraph", 
            "language":"en", 
            "timestamp":1483978498 
        },
        {
            "content": "Erster Abschnitt", 
            "language":"de", 
            "timestamp":1483978498 
        }
    ]
}

But I need to get just a single content field for each data array element, which should be selected by the language. So the result should be (assuming selecting the english content):

{ 
    "_id": "ExxTDXJSwvRbLdtpg",
    "content": "First paragraph"
}

instead of getting all the content data...

I tried to do it with find(c => c.language === 'en), but I don't know how to use this for all elements of the data array. Maybe it is also possible to get the data directly as a mongodb query??

I got an array (as result of a mongoDB query) with some elements like this:

{ 
    "_id": "ExxTDXJSwvRbLdtpg",
    "content": [
        {
            "content": "First paragraph", 
            "language":"en", 
            "timestamp":1483978498 
        },
        {
            "content": "Erster Abschnitt", 
            "language":"de", 
            "timestamp":1483978498 
        }
    ]
}

But I need to get just a single content field for each data array element, which should be selected by the language. So the result should be (assuming selecting the english content):

{ 
    "_id": "ExxTDXJSwvRbLdtpg",
    "content": "First paragraph"
}

instead of getting all the content data...

I tried to do it with find(c => c.language === 'en), but I don't know how to use this for all elements of the data array. Maybe it is also possible to get the data directly as a mongodb query??

Share Improve this question asked Jan 15, 2017 at 13:18 user3142695user3142695 17.4k55 gold badges199 silver badges375 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 3

You could iterate the array and replace the value inside.

var array = [{ _id: "ExxTDXJSwvRbLdtpg", content: [{ content: "First paragraph", language: "en", timestamp: 1483978498 }, { content: "Erster Abschnitt", language: "de", timestamp: 1483978498 }] }];

array.forEach(a => a.content = a.content.find(c => c.language === 'en').content);

console.log(array);

Version with check for content

var array = [{ _id: "ExxTDXJSwvRbLdtpg", content: [{ content: "First paragraph", language: "en", timestamp: 1483978498 }, { content: "Erster Abschnitt", language: "de", timestamp: 1483978498 }] }, { _id: "no_content" }, { _id: "no_english_translation", content: [{ content: "Premier lot", language: "fr", timestamp: 1483978498 }, { content: "Erster Abschnitt", language: "de", timestamp: 1483978498 }] }];

array.forEach(function (a) {
    var language;
    if (Array.isArray(a.content)) {
        language = a.content.find(c => c.language === 'en');
        if (language) {
            a.content = language.content;
        } else {
            delete a.content;
        }
    }
});

console.log(array);

Given that _id and language are input variables, then you could use this aggregate mand to get the expected result:

db.collection.aggregate([{
  $match: {
    _id: _id,
  }
}, {
  $unwind: '$content'
}, {
  $match: {
    'content.language': language,
  }
}, {
  $project: {
    _id: 1,
    content: '$content.content'
  }
}])
var aobjs = [{
  "_id": "ExxTDXJSwvRbLdtpg",
  "content": [
    {
      "content": "First paragraph",
      "language":"en",
      "timestamp":1483978498
    },
    {
      "content": "Erster Abschnitt",
      "language":"de",
      "timestamp":1483978498
    }
  ]
}];

var result = aobjs.map(o => ({ id: o._id, content: o.content.find(c => c.language === 'en').content }));

This returns an object for each with just id and content. In this example, result would be:

[ { id: 'ExxTDXJSwvRbLdtpg', content: 'First paragraph' } ]

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744852493a4597210.html

相关推荐

  • javascript - Select nested array object and replace it - Stack Overflow

    I got an array (as result of a mongoDB query) with some elements like this:{ "_id": "Exx

    2天前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信