0

This works, but question is can I save the naziv.value in var naziv in one go, inside this find method so I don't have to declare another variable?

var naziv = obj.find(c => c.name === "naziv");
console.log(naziv.value)

Current output is as it should be test by console.log(naziv.value), I would like to be just console.log(naziv)

var obj = [{
    name: "naziv",
    value: "test"
  },
  {
    name: "zzz",
    value: "xxx"
  }
]

var naziv = obj.find(c => c.name === "naziv");
console.log(naziv.value)

EDIT: And also to make an array or values if name is the same, example:

var obj = [{
    name: "naziv",
    value: "test"
  },
  {
    name: "zzz",
    value: "xxx"
  },
  {
    name: "Telefon[]",
    value: "tel1"
  }, {
    name: "Telefon[]",
    value: "tel2"
  }
]

var naziv = obj.find(c => c.name === "Telefon[]");
console.log(naziv.value)

Should be: [tel1,tel2]

7
  • 1
    You are counting on name to be unique. Is there a chance it won't be/test for it not being? Commented Sep 24, 2020 at 16:47
  • 1
    var naziv=obj.find(c => c.name === "naziv").value; Commented Sep 24, 2020 at 16:48
  • 1
    I, also, like the de-structuring solution by @Abdullah Razzaki. Commented Sep 24, 2020 at 16:49
  • 2
    If you want to filter and map in the same iteration, just use a for loop: const output = []; for(let o of obj) if(o.name === "naziv") output.push(o.value) Commented Sep 24, 2020 at 16:56
  • 1
    var values=obj.filter(item=>item.name=="Telefon[]").map(item=>item.value) Commented Sep 24, 2020 at 16:59

3 Answers 3

2

You can use de-structuring like const {value:naziv} = obj.find(c => c.name === "naziv");

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

2 Comments

This is great, never seen that, any help with: to make an array if name is not unique, I edited a question
You can use obj.filter(c=>c.name==='naziv').map(c=>c.value) but that will always be an array
1

If you want multiple values to be as an array, this can be one of the ways

var obj = [{name:"naziv",value:"test"},{name:"zzz",value:"xxx"},{name:"Telefon[]",value:"tel1"},{name:"Telefon[]",value:"tel2"}]

var naziv = obj.filter(c => c.name === "Telefon[]").map(res => res.value);
console.log(naziv)

Likewise, if the value is itself is the intended output and if only single value is expected then below is one of the ways. Here, I have used Optional Chaining

var obj = [{name:"naziv",value:"test"},{name:"zzz",value:"xxx"},{name:"Telefon[]",value:"tel1"}]

var naziv = obj.find(c => c.name === "Telefon[]")?.value;
console.log(naziv)

There could be a chance if you need this code to run some older versions of the browsers and there's no support for Optional Chaining then below is yet another way

var obj = [{name: "naziv",value: "test"},{name: "zzz",value: "xxx"},{name: "Telefon[]",value: "tel1"}]

var naziv = (obj.find(c => c.name === "Telefon[]") || {}).value;
console.log(naziv)

var notFound = (obj.find(c => c.name === "not_found") || {}).value;
console.log(notFound);

2 Comments

There is no need for ?. after filter as it will never return null or undefined. It will be an empty array if no matches are found
Yes @adiga. I have updated my answer. Thanks for pointing it. :)
1

var obj = [{
    name: "naziv",
    value: "test"
  },
  {
    name: "zzz",
    value: "xxx"
  },
  {
    name: "naziv",
    value: "xxx"
  }
]

var naziv = obj.filter(item => item.name === 'naziv').map(item => item.value)
console.log(naziv)

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.