1

I have this object of objects:

{
  "0": {
    "boardingGate": "exit_0",
    "departureTerminal": "1",
    "terminalArea": 0,
    "arrivalGate": "enter_0",
    "arrivalTerminal": "2",
    "terminalArea": 0
  },
  "1": {
    "boardingGate": "exit_1",
    "departureTerminal": "1",
    "terminalArea": 0,
    "arrivalGate": "enter_1",
    "arrivalTerminal": "2",
    "terminalArea": 0
  },
  "2": {
    "boardingGate": "exit_0",
    "departureTerminal": "1",
    "terminalArea": 0,
    "arrivalGate": "enter_0",
    "arrivalTerminal": "3",
    "terminalArea": 0
  },
  "3": {
    "boardingGate": "exit_1",
    "departureTerminal": "2",
    "terminalArea": 0,
    "arrivalGate": "enter_1",
    "arrivalTerminal": "3",
    "terminalArea": 0
  }
}

I need to change all "boardingGate" values to "exit_0" and all "arrivalGate" values to "enter_0". And once changed I need to remove the ones that give an equal object structure. The final resultant object I'm looking for would be the following:

{
  "0": {
    "boardingGate": "exit_0",
    "departureTerminal": "1",
    "terminalArea": 0,
    "arrivalGate": "enter_0",
    "arrivalTerminal": "2",
    "terminalArea": 0
  },
  "1": {
    "boardingGate": "exit_0",
    "departureTerminal": "1",
    "terminalArea": 0,
    "arrivalGate": "enter_0",
    "arrivalTerminal": "3",
    "terminalArea": 0
  },
  "2": {
    "boardingGate": "exit_0",
    "departureTerminal": "2",
    "terminalArea": 0,
    "arrivalGate": "enter_0",
    "arrivalTerminal": "3",
    "terminalArea": 0
  }
}

Eliminating in this case one of the first two that would obtain as final result the same data. I have tried with a forEach obtaining the Object.values(data) and I don't get the desired results... and I don't know if there would be an easier way either.

    const tickets = Object.values(data);

    tickets.forEach((next, index, ticket) => {
      const boardingGateKeys: any = Object.keys(next.boardingGate);
      const boardingGateValues: any = Object.values(next.boardingGate);

      boardingGateKeys.forEach((gate, gateIndex) => {
          const arrivalGateKeys: any = Object.keys(gate.outputs);
          const arrivalGateValues: any = Object.values(gate.outputs);
          arrivalGateValues.forEach((output, outputIndex) => {

              });
            }
        });
      });

Thank you very much for your help in advance

1
  • 1
    I have tried with a forEach obtaining the Object.values(data), could you please share the failed attempt? Commented Apr 28, 2020 at 8:53

2 Answers 2

1

You could get the entries, reduce the array by looking to the wanted vommon entries and add for unknown key/value pairs a new data set with updated properties.

Finally create an object from the array.

var data = { 0: { boardingGate: "exit_0", departureTerminal: "1", terminalArea: 0, arrivalGate: "enter_0", arrivalTerminal: "2" }, 1: { boardingGate: "exit_1", departureTerminal: "1", terminalArea: 0, arrivalGate: "enter_1", arrivalTerminal: "2" }, 2: { boardingGate: "exit_0", departureTerminal: "1", terminalArea: 0, arrivalGate: "enter_0", arrivalTerminal: "3" }, 3: { boardingGate: "exit_1", departureTerminal: "2", terminalArea: 0, arrivalGate: "enter_1", arrivalTerminal: "3" } },
    result = Object.assign({}, Object
        .values(data)
        .reduce((r, { boardingGate, arrivalGate, ...o }) => {
            const entries = Object.entries(o);
            if (!r.some(q => entries.every(([k, v]) => q[k] === v))) {
                r.push({ boardingGate: "exit_0", arrivalGate: "enter_0", ...o });
            }
            return r;
        }, [])
    );

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

1 Comment

works perfectly !! now it's my turn to understand!! thank you very much
1

There are two concerns regarding your code:

  • the minor one (both of your input and expected objects are invalid due to duplicating terminalArea key)
  • and the major one - both of your attempt and accepted answer are implementing O(n²)-time algorithms (due to nested loops) which may cause huge performance loss (up to 90% slower compared to O(n)-time algorithm for 1k items), should your input be large enough

So, if you still consider something more comprehensive (and, what's more important, fast), please check out the following approach:

  • build up a hash map that contains unique combination of input object values
  • push remapped object (with uniformed values of boardingGate/arrivalGate) if its hash is missing from the hashmap

The proof of a concept is as follows:

const src = {"0":{"boardingGate":"exit_0","departureTerminal":"1","departureTerminalArea":0,"arrivalGate":"enter_0","arrivalTerminal":"2","arrivalTerminalArea":0},"1":{"boardingGate":"exit_1","departureTerminal":"1","departureTerminalArea":0,"arrivalGate":"enter_1","arrivalTerminal":"2","arrivalTerminalArea":0},"2":{"boardingGate":"exit_0","departureTerminal":"1","departureTerminalArea":0,"arrivalGate":"enter_0","arrivalTerminal":"3","arrivalTerminalArea":0},"3":{"boardingGate":"exit_1","departureTerminal":"2","departureTerminalArea":0,"arrivalGate":"enter_1","arrivalTerminal":"3","arrivalTerminalArea":0}},

      remapDedupe = input => {
        const hashMap = new Set(),
              result = []
        for(idx in input){
          const {boardingGate, arrivalGate, ...rest} = input[idx],
                hash = Object.values(rest).join('|')
          if(hashMap.has(hash)) continue               
          result.push({
            boardingGate: 'exit_0', 
            arrivalGate: 'enter_0', 
            ...rest
          })
          hashMap.add(hash)
        }
        return {...result}
      },
      
      result = remapDedupe(src)
        
console.log(result)
.as-console-wrapper{min-height:100%;}

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.