2

I have an array of dates and an array of objects. I'd like to add the dates to the array of objects as a key value pair {"Date": "10-12-18"}.

dates:

["10-12-18", "10-13-18", 10-14-18"]

data:

[
   {"name":"One", "age": "4"},
   {"name":"Two", "age": "5"},
   {"name":"Three", "age": "9"}
]

I want something like...

[
    {"name":"One", "age": "4", "Date": "10-12-18"},
    ....

How can I do this in TypeScript? I'm used to normal JavaSCript and can't get it right.

Something I have so far:

for (let idx of data){
   data[idx].date = dates[idx] 
}

Thanks!!

4
  • do it like in JS, typescript should compile vanilla JS without problem Commented Aug 27, 2018 at 15:24
  • 2
    Can you post the code you were using? Doing this in Javascript should be no different from doing it in Typescript. So if it wasn't working in JS, you are probably doing something wrong and typescript won't fix that Commented Aug 27, 2018 at 15:25
  • 1
    "I'm used to normal JavaSCript and can't get it right." What have your non-right solutions looked like? Commented Aug 27, 2018 at 15:25
  • Edited with my attempt Commented Aug 27, 2018 at 15:30

5 Answers 5

4

What's wrong with your code is that idx will be the object not the index as you are using for...of. Use a simple regular for like:

for(let idx = 0; idx < data.length; idx++) {
    data[idx].date = dates[idx];
}

Or use forEach to loop one of the arrays and use the index it provides to get the value from the other array:

data.forEach((obj, i) => obj.date = dates[i]);
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect and concise, thank you. I need to get the hang of transitioning into TypeSCript more. it's good to see the two things side by side!
@LaurenAH Actually this is still valid javascript as well.
3
const result = data.map(({ name, age }, index) => ({ name, age, date: dates[index] }));

just map the array to the result.

Comments

2

Something like that:

    let dates = ["10-12-18", "10-13-18", "10-14-18"];

    let objs = [{"name":"One",
     "age": "4"},

     {"name":"Two",
     "age": "5"},

    {"name":"Three",
     "age": "9"}
    ]

    const result = objs.map((item, index) => {
        item.Date = dates[index];
        return item;
    });

console.log(result);

1 Comment

BTW, using map is totally unecessary here as you are altering the original objects so a simple forEach will do. console.log(objs); to see what I'm talking about.
1

According to @Jonas Wilms answer,I think that the map operator is the best solution.

You can also use the spread operator instead of destructuring the object, like this :

const data = [
   { 'name':'One', 'age': '4' },
   { 'name':'Two', 'age': '5' },
   { 'name':'Three', 'age': '9' }
];

const dates = ['10-12-18', '10-13-18', '10-14-18'];

const result = data.map((object, index) => ({ ...object, date: dates[index] }));

In case of long objects, it avoids you to rewrite every key of your object.

Comments

0

In TS, you can do this like below..

var data = [{
    "name": "One",
    "age": "4"
  },

  {
    "name": "Two",
    "age": "5"
  },

  {
    "name": "Three",
    "age": "9"
  }
];

var dates = ["10-12-18", "10-13-18", "10-14-18"];

console.log(data.map((ob, i) => ({ ...ob,
  "Date": dates[i]
})));

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.