1

I have an array, inside this I have objects. I need to work with this and need to access to the object data.

Here is a sample of this array:

var result <-
(16)array […]
​
0:object {…}"myobjectname1": {…}    style_name: "border-top-right-radius"
​​​                                        style_unit: "px"
​​​                                        style_value: "0"
​​​
1:object {…}"myobjectname2": {…} style_name: "border-bottom-right-radius"
        ​​​                                style_unit: "px"
​​​                                        style_value: "0"
​​​
2:object { "myobjectname3": {…} }
​
3:object { "myobjectname4": {…} }
​
4:object { "myobjectname5": {…} }
...

I want to access directly to the different objects by the objectname

Example alert("result.myobjectname1.style_name");

It seem that this could not work, because I don't access the array by the index in this case [0]!?

I don't know this index, it could be different every time.

Is there a way, to directly access the objects by the objectname or in which way, I have to create a array/object that it works?

In php I will do it with a simple array, in JavaScript its not possible because my index is alphanumeric.

UPDATE

This is the way i create the datas in a each-loop

var match=new Array();
each(...)
{
...
  var r = null;
  r={ [myobjectname_from_variable] :  {     'style_name': res_stylename,
                                            'style_value': res_stylevalue,
                                            'style_unit' : elem_styleunit} };

  match.push(r); 
...
}

Important: The value of myobjectname_from_variable is the same as res_stylename and it contains something like "border-top-right-radius"

Here we have to think about, how i can call this in javascript. For example

object.border-top-right-radius.style_value 

wont breack and returns a error of undefined "right"...

2
  • If you're trying to index by border-top-right-radius and it's throwing an error about "right" being undefined, then it's something we aren't looking at. From what we've seen so far, you have objects that are indexed by style names. That particular error might be in how your res_stylevalue is being set, since nothing appears to adjust for a "right" property explicitly. Commented Jan 19, 2019 at 18:33
  • Note the update to my answer. It's the use of hyphens in your keys that are causing issues here. Commented Jan 19, 2019 at 18:39

2 Answers 2

2

You could build an object with all objects of the array. Then take the key of the wanted object for access.

var array = [
        { foo: { id: 1, name: 'foo' } },
        { bar: { id: 2, name: 'bar' } },
        { baz: { id: 3, name: 'baz' } }
    ],
    object = Object.assign(...array);

console.log(object.foo.id);
console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }

For a faster access via a key, you could build your object with a defined object, like

var data = {};

And in the loop, you could assign the value object like this with a property accessor

// inside of the loop
data[myobjectname_from_variable] = {
    style_name: res_stylename,
    style_value: res_stylevalue,
    style_unit: elem_styleunit
};
Sign up to request clarification or add additional context in comments.

9 Comments

i create my datas in a each-loop, how i can create this kind in a each loop too? Did i create in this way a array with a alphanumeric index/key?
so sorry, maybe its a birdy question, what will you tell mit with the "...array"?
spread syntax ... is just part of ES6. actually i do not know how you generate the array. for taking an object, you could have a look to property accessor and add each inner object to a new property.
i've update my code with the way how i create my array - could you have a look please? I could not see a big difference?
do you need the array?
|
0

Your best bet is to iterate the array and then call the index of each iteration. There's also an array method called .forEach that wraps this up for you.

var yourArray = []; // contains the array of objects you mentioned
yourArray.forEach(object => {
    const key = Object.keys(object)[0];
    alert(object[key].style_name);
})

Update

Your main issue is the use of hyphens in your keys. If you want to index an object like that then you'll need to reference the keys as strings.

const obj = {'border-top-right-radius':'style_value'};
const test = obj['border-top-right-radius'];
console.log(test); // this logs 'style_value '

const other = obj.border-top-right-radius; 
// This will throw that 'right' is undefined.

If you want that format then change your hyphens to underscores. With underscores, there's no issue directly referencing them as you desire. You can easily remap to the hyphenated string later for what you're doing with .replace

const underscoreReplacement = 'border_top_right_radius';
const actualSheetValue = underscoreReplacementreplace(/_/g, '-');
console.log(actualSheetValue); // logs 'border-top-right-radius'

4 Comments

correct me if i'm wrong, searching for every time i call a needed data from the array/object looks for me as a really slow way?
Setting everything into an object is definitely faster, but I assumed that since your data lives inside of an array that there was some issue indexing into an object. For instance is it possible that the array could contain multiple objects with the same name? If two objects had myobjectname1 as a key then only the contents of the last object would appear.
no, under normal conditions, must the alphanumeric index unique. I'm looking for a way, to work effeicent with the datas - in wich i have to create them its the part to check here...
In that case, the object is the way to go. This is a safe route if you can't guarantee uniqueness or you have multiple entries of similar types. It's still doable to implement even that as an object but then you're creating a nested object based on some other key, like a counter - or some other approach that makes it unwieldy to handle.

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.