-1

I just know a little bit of javascript and appreciate if anyone can help for my question. I have an array like this:

var data = [
            {a:44,b:2},
            {a:50,b:5},
            {a:49,b:7},
            {a:41,b:6},
            {a:59,b:9},
            ]

I want to intert a column a1(a1=a-b) in the array.And intert name in only the first two rows of array. Eventually, I want to get the result like this:

var data = [
            {a:44,b:2,a1:42,name:"monkey"},
            {a:50,b:5,a1:45,name:"dog"},
            {a:49,b:7,a1:42},
            {a:41,b:6,a1:35},
            {a:59,b:9,a1:50},
            ]

Since I have thousands of rows, it's impossible to do it manually. Can anyone help to write down the code of this transition? Many thanks!

yxz122930
  • 21
  • 9

3 Answers3

1

This goes under the assumption that a and b are static object keys! Please note that objects do not preserve order.

data.forEach(obj => obj["a1"] = obj.a - obj.b);

This one liner sets a1 of the current object equal to a-b (+ before the value is converting to a number, just in case strings get in there (sort of redundant). To add in the name, just add brackets to the arrow function (will need a second line, so readability), and just add obj["name"] = "whatever";

Sterling Archer
  • 20,452
  • 15
  • 77
  • 107
1

Just one line of code, or so:

var data = [
        { a: 44, b: 2 },
        { a: 50, b: 5 },
        { a: 49, b: 7 },
        { a: 41, b: 6 },
        { a: 59, b: 9 },
    ],
    insert = ['monkey', 'dog'];

data.forEach(function (a, i) {
    a.c = a.a - a.b;
    if (i in insert) {
        a.name = insert[i];
    }
});
document.body.innerHTML = '<pre>' + JSON.stringify(data, 0, 4) + '</pre>';
Nina Scholz
  • 323,592
  • 20
  • 270
  • 324
  • 1
    Nice answer but I wouldn't suggest using `document.write` ever. Just use a plain old `console.log(data)` then look in the console (hit F12). – Mike Cluck Dec 08 '15 at 19:40
  • i am not a friend of console, because here it's not necessary. – Nina Scholz Dec 08 '15 at 19:43
  • 1
    @NinaScholz the console is the most powerful tool a JS developer has. Let the console in. Give it a hug. Embrace the console. – Sterling Archer Dec 08 '15 at 19:44
  • Using `document.write` is [very bad practice](http://stackoverflow.com/questions/802854/why-is-document-write-considered-a-bad-practice) and once I started using the console, I never went back. It really improves everything. – Mike Cluck Dec 08 '15 at 19:45
  • @MikeC, please supply some alternative. – Nina Scholz Dec 08 '15 at 21:23
  • @SterlingArcher, me and the console, hm, for here in closed result situation, it's not necessary. – Nina Scholz Dec 08 '15 at 21:25
  • @NinaScholz The console IS the alternative. It's up to you if you use it but you really, really should start using it. It's not hard at all. Literally, you just hit F12 and you can see stuff instead of writing it to the page every time you need to log something out. Writing to the page can cause conflicting styles and you have to search for it at the bottom of the page somewhere instead of just looking in one central location. Either way, do as you please :) – Mike Cluck Dec 08 '15 at 21:27
-1

loop over the first two objects and add your preferred attributes

var data = [
    {a:44,b:2},
    {a:50,b:5},
    {a:49,b:7},
    {a:41,b:6},
    {a:59,b:9},
];

for (var i = 0; i < data.length; i++) {
    data[i].a1 = data[i].a - data[i].b;
    if (i == 0) { data[i].name = "monkey"; }
    if (i == 1) { data[i].name = "dog"; }
}
Mike Cluck
  • 28,921
  • 12
  • 72
  • 85
Jay
  • 625
  • 8
  • 33