2

I have an array of objects, and each object might include properties that are named the same. How do I iterate over the objects property to extract each entry?

I've tried the code below but it only gives me the last property in the object. Any ideas?

var clearingnumberz = [{
    "Namn": "Nordea",
    "Clearingnummer": "1100-1199",
    "Clearingnummer": "1400-2099",
    "Clearingnummer": "3000-3399",
    "Clearingnummer": "3410-4999"
  },
  {
    "Namn": "Danske Bank",
    "Clearingnummer": "1200-1399",
    "Clearingnummer": "9180-9189"
  },
  {
    "Namn": "Handelsbanken",
    "Clearingnummer": "6000-6999"
  },
  {
    "Namn": "JP Nordiska",
    "Clearingnummer": "2300-2309"
  }
];


function SearchForBankName() {

  var userInput = document.getElementById("bankTxt").value;

  for (var bank in clearingnumberz) {

    var currentObject = clearingnumberz[bank];

    if (userInput === currentObject.Namn) {

      for (var clearingnumber in currentObject) {
        console.log(currentObject.Clearingnummer);
      }
    }
  }
}
<input id='bankTxt' placeholder="search a value in Bank" value="Handelsbanken">
<input type="button" onclick="SearchForBankName()" value="search">

5
  • it is not valid javascript with the same key in an object. Commented Oct 19, 2016 at 10:13
  • objects can not have duplicate keys Commented Oct 19, 2016 at 10:14
  • keep value as array for properties with multiple values, eg: "Clearingnummer": ["1400-2099", "1400-4099", "1600-2129"], Commented Oct 19, 2016 at 10:15
  • you can use forEach on object to get key and value both for properties of object. Commented Oct 19, 2016 at 10:15
  • THanks for the array-tip, that seems like a good solution. I split the intervals in to two strings to be able to search through the interval 1400-2099. Can you do this easily on an array aswell? :O Commented Oct 19, 2016 at 10:24

2 Answers 2

1

No, you don't. JavaScript objects may not have properties with the same name. So your objects don't actually look like that.

const obj = {
  "Namn": "Nordea",
  "Clearingnummer": "1100-1199",
  "Clearingnummer": "1400-2099",
  "Clearingnummer": "3000-3399",
  "Clearingnummer": "3410-4999"
}

console.log(obj); // oops

In short, you can't do what you want. You have to change the way your data is modeled.

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

Comments

1

Fo a faster access, you could convert your data (with arrays) to a hash table and get the name with the number as key.

var clearingnumberz = [{ namn: "Nordea", clearingnummer: ["1100-1199", "1400-2099", "3000-3399", "3410-4999"] }, { namn: "Danske Bank", clearingnummer: ["1200-1399", "9180-9189"] }, { namn: "Handelsbanken", clearingnummer: ["6000-6999"] }, { namn: "JP Nordiska", clearingnummer: ["2300-2309"] }],
    numberz = Object.create(null);

clearingnumberz.forEach(function (bank) {
    bank.clearingnummer.forEach(function (clearingnumber) {
        numberz[clearingnumber] = bank.namn;
    });
});

console.log(numberz['6000-6999']);
console.log(numberz);    

Comments

Your Answer

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