0

I have this JSON data object:

    var dataObjects = [
         {
          "Name": "Date & Time",
          "Type": "Date",
          "Value": "2019-12-11"
          },
          {
          "Name": "Activity",
          "Type": "String",
          "Value": "ADD"
          }
      ]

I want to build a new JSON data array object out of this one except the new one has the date formatted from "2019-12-11" to "December 11, 2019".

Here is my attempt after googling around but I'm getting a lot of syntax error.

Please excuse my lack of knowledge in Typescript and Javascript, that's why I ask for help because something simple to you still gives me a lot of difficulties.

        public FunctionA(dataObjects: any[]): object 
        {
           let returnObj: any = {}
           let returnObjArray: any = [];

           for(let obj in dataObjects){

             var dateValue = obj.Date;

             if(obj.Type == "Date"){
               dateValue = obj.Date.Format()
             }
             returnObj = {"Name", obj.Name, "Type": obj.Type, "Value": dateValue };
             returnObjArray.push(returnObj);
           }

           return returnObjArray;
        }

I sort of understand my error is that I somehow has to specify what is this "any" array before I can use its property. However, I tried declare it below and I still have error:

    dataObjects: Array[{ Name: string, Type: string, Value: string }]

3 Answers 3

1

This is how you can achieve your goal:

 var dataObjects = [
    {
    "Name": "Date & Time",
    "Type": "Date",
    "Value": "2019-12-11"
    },
    {
    "Name": "Activity",
    "Type": "String",
    "Value": "ADD"
    }
]
type dataType = {Name: string, Type: string, Value: string};

function A(dataObjects: dataType[]): dataType[] 
{
    let returnObjArray: dataType[] = [];

    dataObjects.forEach((obj:dataType)  => {
        if(obj.Type === "Date"){
        obj.Value = moment(obj.Value).toString("MMM dd, yyyy");
        }
        returnObjArray.push(obj);
    });
    return returnObjArray;
}

moment(obj.Value).toString("MMM dd, yyyy"); is where you can convert your old date string to your desired format (using moment package).

Sign up to request clarification or add additional context in comments.

Comments

1

The syntax is this:

Array<{ Name: string, Type: string, Value: string }>

or this:

{ Name: string, Type: string, Value: string }[]

But even better is to give it a name:

type MyObject = {
 Name: string,
 Type: string,
 Value: string 
}

And then use MyObject[] everywhere.

Comments

1

I think the best way to do it is to create your own DataObject interface and use the map function to create your new array.

interface DataObject {
  name: string;
  type: string;
  value: string;
}

var dataObjects: DataObject[] = [
  {
   "name": "Date & Time",
   "type": "Date",
   "value": "2019-12-11"
   },
   {
   "name": "Activity",
   "type": "String",
   "value": "ADD"
   }
]

function convertDate(date: string): string {
  // Your conversion code here.
  return date;
}

function convertDataObjects(dataObjects: DataObject[]): DataObject[] {
  return dataObjects.map(obj => {
    return {name: obj.name, type: obj.type, value: convertDate(obj.value)};
  });
}

console.log(convertDataObjects(dataObjects));

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.