Is there a way we can create an array in JQuery with key and values?
I want to loop through the TFNDailySummaryReportData
array and assign its DateWiseSalesData's Name property to key and DateWiseSalesData as value.
for (var i = 0; i < TFNDailySummaryReportData.length; i++)
{
var keyValueArray = {
Key: TFNDailySummaryReportData[i].DateWiseSalesData.Name;
value: TFNDailySummaryReportData[i].DateWiseSalesData
}
}
Is it possible to achieve something like this? If yes, how?
Is there a way we can create an array in JQuery with key and values?
I want to loop through the TFNDailySummaryReportData
array and assign its DateWiseSalesData's Name property to key and DateWiseSalesData as value.
for (var i = 0; i < TFNDailySummaryReportData.length; i++)
{
var keyValueArray = {
Key: TFNDailySummaryReportData[i].DateWiseSalesData.Name;
value: TFNDailySummaryReportData[i].DateWiseSalesData
}
}
Is it possible to achieve something like this? If yes, how?
Share Improve this question edited Dec 29, 2016 at 9:27 Satpal 133k13 gold badges167 silver badges170 bronze badges asked Dec 29, 2016 at 9:20 Fact FinderFact Finder 1913 gold badges5 silver badges15 bronze badges 1- keyValuesArray.push("TFNDailySummaryReportData[i].DateWiseSalesData.Name" : "TFNDailySummaryReportData[i].DateWiseSalesData") – 404answernotfound Commented Dec 29, 2016 at 9:23
2 Answers
Reset to default 3You were very close, You can define an array and use .push()
method to populate it.
var arr=[];
for (var i = 0; i < TFNDailySummaryReportData.length; i++)
{
arr.push({
Key: TFNDailySummaryReportData[i].DateWiseSalesData.Name;
value: TFNDailySummaryReportData[i].DateWiseSalesData
})
}
You can also use .map()
var arr=TFNDailySummaryReportData.map(function(value){
return {
Key: value.DateWiseSalesData.Name;
value: value.DateWiseSalesData
}
});
This has nothing to do with jQuery. Arrays are basic JavaScript, and you can map the KVP based objects to a new array as such:
Using ES6:
const keyValueArray = TFNDailySummaryReportData.map(x => {
return { [x.DateWiseSalesData.Name]: x.DateWiseSalesData };
});
Using ES5:
var keyValueArray = TFNDailySummaryReportData.map(function(x) {
return { x.DateWiseSalesData.Name : x.DateWiseSalesData };
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745311387a4622014.html
评论列表(0条)