How do you append a key called 'Index' with a value being the dictionaries index to the below array? The array should be sorted by Timestamp before this occurs so that TimeStamp:111 comes first and TimeStamp:222 comes second, etc.
For example, see below for original array
items = [
{Id: "01", Name: "A", Price: "1.00", Quantity: "1",TimeStamp:111},
{Id: "02", Name: "B", Price: "10.00", Quantity: "1",TimeStamp:222},
{Id: "04", Name: "C", Price: "9.50", Quantity: "10",TimeStamp:434},
{Id: "03", Name: "a", Price: "9.00", Quantity: "2",TimeStamp:545},
{Id: "06", Name: "b", Price: "100.00", Quantity: "2",TimeStamp:676},
{Id: "05",Name: "c", Price: "1.20", Quantity: "2",TimeStamp:777}
];
The resulting array should look like this:
items = [
{Id: "01", Name: "A", Price: "1.00", Quantity: "1",TimeStamp:111,Index:1},
{Id: "02", Name: "B", Price: "10.00", Quantity: "1",TimeStamp:222,Index:2},
{Id: "04", Name: "C", Price: "9.50", Quantity: "10",TimeStamp:434,Index:3},
{Id: "03", Name: "a", Price: "9.00", Quantity: "2",TimeStamp:545,Index:4},
{Id: "06", Name: "b", Price: "100.00", Quantity: "2",TimeStamp:676,Index:5},
{Id: "05",Name: "c", Price: "1.20", Quantity: "2",TimeStamp:777,Index:6}
];
Here's my attempt: http://jsfiddle.net/chrisguzman/f7dnfrjf/ I've figured out how to append an item to each dictionary, but not how to append its respective iterator.
Also, here it is in angular js (bonus points if there is a way to do it with angular): http://codepen.io/chriscruz/pen/GgjGvO?editors=101
Index started from 0, so you need add +1
for each index, like this
items.forEach(function (d, index) {
d.Index = index + 1;
});
Demo: http://jsfiddle.net/f7dnfrjf/1/
Angular demos:
http://codepen.io/pen/ZYpRag - indexes are displayed as 2, 3, 5, 6
http://codepen.io/pen/zxKapG - indexes are displayed as 1, 2, 3, 4
The runtime system passes 3 parameters to .forEach()
: the first is the array element, the second is the index, and the third is the array itself.
Thus:
items.forEach(function (d, i) {
d.Index = i;
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.