-1

I know this has been asked before but I can't seem to find the answer, i want to get no_anggota, nama_lahir and tgl_lahir from array isiuser, and i want to insert to ZZZ .

isiseat = [
    [{ x: 0, y: 0, val: 1 },
    { x: 1, y: 0, val: 1 },
    { x: 2, y: 0, val: 1 }]
    [{ x: 0, y: 1, val: 1 },
    { x: 1, y: 1, val: 1 },
    { x: 2, y: 1, val: 1 }]
    [{ x: 0, y: 2, val: 1 },
    { x: 1, y: 2, val: 1 },
    { x: 2, y: 2, val: 1 }]
];
isiuser = [ { no_anggota: '12345', nama_lahir: ' Agus dwi', tgl_lahir: '04-09-2018'}, 
            { no_anggota: '12345', nama_lahir: 'Septano', tgl_lahir: '15-08-2018'},
            { no_anggota: '12345', nama_lahir: 'Septian putra', tgl_lahir: '15-08-2018'}
        ];

isiseat.forEach((item1) => {
    item1.forEach((item2) => {
        var detaildata = {};
        detaildata.x = item2.x
        detaildata.y = item2.y
        detaildata.val = item2.val
        detaildata.no_anggota = 'ZZZ'
        detaildata.nama_lahir = 'ZZZ'
        detaildata.tgl_lahir = 'ZZZ'
        detaildata.jadwal = req.body.jadwal
        detaildata.section = req.body.section
        var savedata = new DetailBooking(detaildata);
        savedata.save()
    })
});

4
  • 1
    Please re-read your questions and make proper corrections as it is not making any sense as of now. Commented Aug 1, 2018 at 11:17
  • 1
    If you can add your desired result, it would make easier to understand. Commented Aug 1, 2018 at 11:20
  • i wan to like this i.imgur.com/LayDNY1.png Commented Aug 1, 2018 at 11:27
  • 1
    isiseatneeds , between entries Commented Aug 1, 2018 at 11:28

2 Answers 2

0

You can use bulk insert bulk insert

var dataArray = [];
var isiseat = [
            [
                { x: 0, y: 0, val: 1 },
                { x: 1, y: 0, val: 1 },
                { x: 2, y: 0, val: 1 }
            ],
            [
                { x: 0, y: 1, val: 1 },
                { x: 1, y: 1, val: 1 },
                { x: 2, y: 1, val: 1 }
            ],
            [
                { x: 0, y: 2, val: 1 },
                { x: 1, y: 2, val: 1 },
                { x: 2, y: 2, val: 1 }
            ]
        ]
var bulk = db.items.initializeUnorderedBulkOp();
isiseat.forEach((item1) => {
    item1.forEach((item2) => {
        var detaildata = {};
        detaildata.x = item2.x
        detaildata.y = item2.y
        detaildata.val = item2.val
        detaildata.no_anggota = 'ZZZ'
        detaildata.nama_lahir = 'ZZZ'
        detaildata.tgl_lahir = 'ZZZ'
        detaildata.jadwal = 'sssss'
        detaildata.section = 'sssssss'
        bulk.insert(detaildata);
    })
});
bulk.execute();

my saved data look like this

    {
    "_id" : ObjectId("5b61aab063727f69dc5ad1b8"),
    "x" : 0.0,
    "y" : 0.0,
    "val" : 1.0,
    "no_anggota" : "ZZZ",
    "nama_lahir" : "ZZZ",
    "tgl_lahir" : "ZZZ",
    "jadwal" : "sssss",
    "section" : "sssssss"
}

{
    "_id" : ObjectId("5b61aab063727f69dc5ad1b9"),
    "x" : 1.0,
    "y" : 0.0,
    "val" : 1.0,
    "no_anggota" : "ZZZ",
    "nama_lahir" : "ZZZ",
    "tgl_lahir" : "ZZZ",
    "jadwal" : "sssss",
    "section" : "sssssss"
}
Sign up to request clarification or add additional context in comments.

Comments

0

If both isiseat and isiuser arrays are of the same length, you can pass the index parameter to the forEach function. (See "Loop through an two arrays simultaneously")

Therefore you could pass the index to your first forEach function:

// Index passed here
isiseat.forEach((item1, index) => {
  item1.forEach((item2) => {
    var detaildata = {
      x: item2.x,
      y: item2.y,
      val: item2.val,
      // ...And then access the `isiuser` array accordingly 
      no_anggota: isiuser[index].no_anggota,
      nama_lahir: isiuser[index].nama_lahir,
      tgl_lahir: isiuser[index].tgl_lahir,
      jadwal: req.body.jadwal,
      section: req.body.section
    };
    // console.log to see results
    console.log(detaildata)
    var savedata = new DetailBooking(detaildata);
    savedata.save()
  })
});

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.