1

With 2 arrays (one of numbers and other of objects),
How can I get a specific object's-(size) value (in other function) without knowing its index (and it's value) in the array only its key
with the shortest and fastest way so I can use that value number ?

If the array is empty except for this specific object, the size property value will be - 0;

If my question isn't clear please comment and I'll fix it and make it betters.

let firstArray = [ 1, 2 , 10, 23, {size: 890}];
     
   funcExtract(firstArray);
    
   function funcExtract(arr){
        
    // arr ? How to find the object when knowing only its key
    }

And with array of objects

let secondArray  = [ {a:2}, {b:4 },{size: 700}, {c:5} ];
 
      funcExtract(secondArray); 

      function funcExtract(arr){
        
      }
4
  • Please remove reactjs from the post tags. It's not relevant for what you're asking. Commented Aug 12, 2021 at 10:53
  • Your first array has numbers and an object that has a size property. What is the "specific object" you're looking for? Can you give some examples? Commented Aug 12, 2021 at 10:53
  • But what specific object? What is the "key" you have? Commented Aug 12, 2021 at 10:59
  • {size: 890} in this case the key is "size", and the value is 890 Commented Aug 12, 2021 at 11:02

3 Answers 3

2

You can use the Array#find function:

const test = [1, 2, 3, 4, {key: 5}]
const findOne = test.find(item => item.hasOwnProperty("key"))
console.log(findOne)

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

2 Comments

I meant I don't need know the key's value. important
What do you mean by knowing only its key then?. Updated answer, take a look
1

Here's the thing which you can do it:

let key = "size"
let secondArray  = [ {a:2}, {b:4 },{size: 700}, {c:5} ];
let firstArray = [ 1, 2 , 10, 23, {size: 890}];
function funcExtract(arr,key){
   let filter = arr.filter((x)=> typeof x === "object" && x.hasOwnProperty(key))
   console.log(filter[0].size)
}
funcExtract(firstArray,key);
funcExtract(secondArray,key); 

1 Comment

Yes your way works, but I found here to better use the find method, it's a shorter way. Thanks
0
let secondArray  = [ {a:2}, {b:4 },{size: 700}, {c:5} ];
secondArray.find(o => o.size) // {size: 700}
a.filter(o => o.size) // good if you need to find more than 1 with same key

5 Comments

What if o.size is 0 (i.e. falsey)?
well presumably the OP can add their own safeguards, think that's out of the scope of the question
@RedBaron I updated my question, regards to size = 0
I'm not sure I understand the new question. does my answer not work? and if not, which part does not work?
@RedBaron When {size: 0} it will be > undefined. Than I will use find(o => o.hasOwnProperty("size")); Thanks

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.