Witam wszystkich. Mam mały problem z zapisem danych do obiektu.
Na stronie posiadam zagnieżdzoną listę nie numerowaną i muszę z niej wyciągnąć "data-name" i tekst z listy zagnieżdzonej, oczywiście w odpowiednim formacie:
{
"field1":{
"message" : {
"0":"Notification 1",
"1":"Notification 2",
},
"status":true
},
"field2":{
"message" : {
"0":"Notification 3",
"1":"Notification 4",
},
"status":true
}
}
ale utknąłem w miejscu gdzie tekst mam umieścić w węźle message. Poniżej podaję kod:
Lista
<ul id="notification">
<li><h4 data-name="field1">Name Field 1</h4>
<ul>
<li>Notification 1</li>
<li>Notification 2</li>
</ul>
</li>
<li><h4 data-name="field2">Name Field 2</h4>
<ul>
<li>Notification 3</li>
<li>Notification 4</li>
</ul>
</li>
</ul>
Skrypt JS (jQuery)
var listNotification = $('#notification');
var listNotificationFieldName = listNotification.find('h4');
var listFields = {};
listNotificationFieldName.each(function(){
var filedName = $(this).attr('data-name')
listFields[filedName] = {};
if($(this).next('ul').length) {
$(this).next('ul').find('li').each(function(i, elem){
var notise = $(this).text();
///////// W tym miejscu nie wiem jak to rozwiązać
listFields[filedName][i] = notise;
/////////
listFields[filedName].status = true;
})
}
});
console.log(JSON.stringify(listFields))
Obecny wynik:
{
"field1":{
"0":"Notification 1",
"1":"Notification 2",
"status":true
},
"field2":{
"0":"Notification 3",
"1":"Notification 4",
"status":true
}
}
Z góry dzięki za pomoc
Pozdrawiam