-3

I was calling a function and pass array of objects as first argument and second argument was object property of first argument but I don't know why map func doesn't accepting second argument property Here code plz see it once

const myfunc = (arrObj, property) => {
  const arr1 = arrObj.map(item => {
    return item.property
  }
  return arr1:
}

const arrObj = [{
    title: 'book',
    body: 'hello'
  },
  {
    title: 'cup',
    body: 'hii'
  }
];

// Func call
console.log(myfunc(arrObj, 'title'));
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64
  • You should be using lowercase `const` and `return`. But the key problem is you're not actually returning anything from `myfunc` to log. – Andy Jul 20 '21 at 08:24
  • Please fix the casing of your code (JavaScript is case-sensitive and `const`/`return` are lowercase) and the syntax error(s) (your `console.log` line is missing a parenthesis). Also please add what exactly you expect this code to do. – Ivar Jul 20 '21 at 08:25
  • 1
    You also need to access the property with `item[property]` otherwise it is just looking for a property called `property` – Alastair Jul 20 '21 at 08:26

2 Answers2

3
  1. Use lowercase for const, return, console.

  2. Return a value (an array, in this case) from your function that you can log.

  3. Use item[property] to access the title property. There is no "property" key in those objects.

  4. Make sure you close all of your parentheses.

function myfunc(arrObj, property) {
  return arrObj.map(item => item[property]);
}

const arrObj=[{title:"book",body:"hello"},{title:"cup",body:"hii"}];

console.log(myfunc(arrObj, 'title'));
Andy
  • 61,948
  • 13
  • 68
  • 95
2

There are multiple errors in your code. First keywords like return,const,console are all case sensative. Secondly you are not returning from the function. Third since property is a variable you have to use square braces instead of dot

const myfunc = (arrObj, property) => {
  return arrObj.map(item => {
    return item[property]
  })
}

const arrObj = [{
    title: 'book',
    body: 'hello'
  },
  {
    title: 'cup',
    body: 'hii'
  }
];

// Func call
console.log(myfunc(arrObj, 'title'));
brk
  • 48,835
  • 10
  • 56
  • 78