0

I am working on an angular application. I have to read data from json and process in two parts.

  1. Read data from json using express: I have following Json in alphabetically sorted order.

    { "1": "Andy", "2": "Billy" "3": "Carry" "4": "Doda" }

Left hand side are Id's and Right one's are names. I have to read this data using express and access this data in my angular component.

  1. In my angular component I have another array which is also sorted. That array is as follows:

    [
           {
             "Name": "Andy",
             "Id": "1"
             "IncomingTime": "2020-06-19T11:02+00:00",
             "Outgoingtime": "2020-06-19T11:07+00:00"
           },
           {
             "Name": "Billy",
    
             "Id": "2"
             "IncomingTime": "2020-06-19T11:05+00:00",
             "Outgoingtime": "2020-06-19T11:17+00:00"
           },
           {
             "Name": "Ciena",
             "Id": 5
             "IncomingTime": "2020-06-19T11:05+00:00",
             "Outgoingtime": "2020-06-19T11:07+00:00"
           },
           {
             "Name": "Doda",
             "Id": "4"
             "IncomingTime": "2020-06-19T11:05+00:00",
             "Outgoingtime": "2020-06-19T11:07+00:00"
           }
         ]
    

Once above Json data is available in component(JSON in point 1), I have to make a new array(or any data structure which can be sorted). In that new array I have to put data in such a way that alphabetically sorted "Name" coming from above Json should match with with data in sorted array. If Name is matched then content(Name, id, IncomingTime , OutgoingTime) is copied to new array for that particular "Name" which will also be sorted. If suppose While comparing names we came across a name for which data is not there in sorted array, we need to keep only Name in new array doesn't matter data is there or not. Like in above data for "Carry" data is not there in sorted array still in final array "Carry" should be there. How can I do that?

1
  • check my answer Commented Jun 26, 2020 at 7:26

1 Answer 1

1

You can do it using Lodash. Lodash provides functions to achieve what you want. documentation is here.

install Lodash from here.

import Lodash import * as _ from 'lodash';

I think this is what you want

const arr = { 1: 'Andy', 2: 'Billy', 3: 'Carry', 4: 'Doda' };

const data = [
    {
        "Name": "Andy",
        "Id": "1",
        "IncomingTime": "2020-06-19T11:02+00:00",
        "Outgoingtime": "2020-06-19T11:07+00:00"
    },
    {
        "Name": "Billy",
        "Id": "2",
        "IncomingTime": "2020-06-19T11:05+00:00",
        "Outgoingtime": "2020-06-19T11:17+00:00"
    },
    {
        "Name": "Ciena",
        "Id": 5,
        "IncomingTime": "2020-06-19T11:05+00:00",
        "Outgoingtime": "2020-06-19T11:07+00:00"
    },
    {
        "Name": "Doda",
        "Id": "4",
        "IncomingTime": "2020-06-19T11:05+00:00",
        "Outgoingtime": "2020-06-19T11:07+00:00"
    }
    ];

const newArray  = _.map(arr, item => {
    const value = _.find(data, ['Name', item]);
    const obj = value ? value : {Name: item};

    return obj;
});

console.log(newArray);
Sign up to request clarification or add additional context in comments.

4 Comments

the map function is used to create a new object. I passed 'arr' to map function which will iterate on each value in 'arr'. find is used to check if an 'item' is present in 'data'. if not present then the value is undefined so I am assigning a new object to 'obj' else value.
you can add a breakpoint in map function to check working.
It is working but not as expected because when I send data to my component first time, this code gives correct results but when I send data for seconf time without refreshing it keeps previous data and appends the new one. This is the problem.
when you send data second-time empty newArray = [] like this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.