0

I want to check if some button is clickable according to some requirements but I can't even make the start of the function works.

I have this object array. Each object is a button and some of them have a requirement. Eg: for story to be clickable it need 10 ideas, so story.ideaReq = 10. To get grana, you need to have 1 story, so grana.storyReq = 1.

My code should return:

story ideaReq 10

that is:

story.name = story,
req.name+'Req' = ideaReq and
story['ideaReq'] = 10

But it returns:

story ideaReq undefined

If I console.log(story.ideaReq) it works perfectly but I need to be able to call this function (as I'll end having more objects than those above).

What am I missing?

const numbers = [
  time = {
    'name': 'time',
    'in': 0,
    'val': 0
  },
  idea = {
    'name': 'idea',
    'in': 0,
    'val': 10,
    'time': 1
  },
  story = {
    'name': 'story',
    'in': 0,
    'val': 1,
    'time': 4,
    'ideaReq': 10
  },
  grana = {
    'name': 'grana',
    'in': 0,
    'val': 1,
    'time': 1,
    'storyReq': 1
  }
];


var checkButton = (button, req) => {

  let name = button.name,
    prop = req.name + 'Req';

  console.log(name, prop, name['prop']);
}

checkButton(story, idea);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
angelod1as
  • 95
  • 1
  • 11
  • 2
    Your array syntax is invalid. Arrays contain entries separated with commas, not name/value pairs. Your syntax is a bit like an object initializer, but you're using `=` where you would use `:`. So `const numbers = [ {name: 'time', in: 0, out: 0}, {/*...*/}];` (Note that I haven't put the property names in any quotes; you *can*, if you like, either single or double quotes are fine.) Also note that `const` and a few other things you're using are new as of ES2015 (aka "ES6"), so if you're not transpiling, it will only work on quite up-to-date JavaScript engines. – T.J. Crowder Aug 14 '16 at 15:50
  • Or you could use an object: `const numbers = {time: {name: 'time', in: 0, out: 0}, idea: {/*...*/} };` – T.J. Crowder Aug 14 '16 at 15:51
  • 1
    If you use an actual array, your question is answered [here](http://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript). If you use an object, your question is answered [here](http://stackoverflow.com/questions/18898743/javascript-loop-through-object-properties). – T.J. Crowder Aug 14 '16 at 15:53
  • 1
    Finally: There is no JSON in your question at all, so I've removed the tag. The [tag:function] tag was also irrelevant. – T.J. Crowder Aug 14 '16 at 15:53
  • Syntax error , u can verify yours javascript object in http://jsonlint.com/ – Nihar Sarkar Aug 14 '16 at 16:06
  • @T.J.Crowder The array syntax is valid. They are using a shorthand to create variables to store the elements, and then adding those elements to the array. – 4castle Aug 14 '16 at 16:33
  • @4castle: It's *possible* they're doing that, although if so they'll need to declare those variables before the code shown, which they haven't shown that they're doing. I suspect it's much more likely that it's just written incorrectly. But you're right, given declared variables, that would be a valid array initializer, as the result of an assignment is the value assigned. (Unless, of course, they're in loose mode and falling prey to [*The Horror of Implicit Globals*](http://blog.niftysnippets.org/2008/03/horror-of-implicit-globals.html).) – T.J. Crowder Aug 14 '16 at 16:35
  • @NiharSarkar: jsonlint verifies JSON, not JavaScript. – T.J. Crowder Aug 14 '16 at 17:07
  • @T.J.Crowder Thanks for the edits, I'm kinda new here. Doubts: I want to access my objects like `numbers.time.val` and, at the same time, be able to iterate through this array in a for loop later (so I need the indexes, that's why the array). I'm also using babel to transpile the code. – angelod1as Aug 14 '16 at 20:36
  • @4castle I did add the variables declarations before the array as mentioned. I'm doing what you said. I want an array of variables, in which each one is an object with properties. – angelod1as Aug 14 '16 at 20:38
  • If you use an object instead of an array, you can use a [`for...in`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) loop, or [`Object.keys()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys) to iterate over the values. To do `numbers.time.val` you would have to use an object instead of an array. – 4castle Aug 15 '16 at 02:21

1 Answers1

0

Your code is trying to access a property prop of name. Instead, you should access the property of button with the string stored by prop.

Change

console.log(name, prop, name['prop']);

To

console.log(name, prop, button[prop]);

const numbers = [
  time = {
    'name': 'time',
    'in': 0,
    'val': 0
  },
  idea = {
    'name': 'idea',
    'in': 0,
    'val': 10,
    'time': 1
  },
  story = {
    'name': 'story',
    'in': 0,
    'val': 1,
    'time': 4,
    'ideaReq': 10
  },
  grana = {
    'name': 'grana',
    'in': 0,
    'val': 1,
    'time': 1,
    'storyReq': 1
  }
];


var checkButton = (button, req) => {

  let name = button.name,
    prop = req.name + 'Req';

  console.log(name, prop, button[prop]);
}

checkButton(story, idea);

Also, if you don't declare your variables, they will be globals, so before your array initializer, you should add:

let time, idea, story, grana;
4castle
  • 32,613
  • 11
  • 69
  • 106