Angular/Vue.js loop through two objects in same directive

user909410

I don't know if what I'm trying to do is possible. I'm trying to create an Angular directive that repeats over a data object and prints out its values as well as the values of a second unrelated object with similar structure.

I'm working on a translation app where the master version of the language file is shown in one column and the translation is shown in the next. I would like to repeat through the master object and then also show the translation where there is one. I do not want to merge the two objects, because I would prefer to maintain two-way binding between the translation object and the DOM so it can be saved easily.

This is very simply what I'm trying to do:

Objects

var master: {
    face: {
        a: aaa,
        b: bbb,
        c: ccc,
        more: {
            d: ddd,
            e: eee
        }
    },
    magic: magic,
    test: test
}

var translation: {
    face: {
        c: cccc,
        more: {
            d: dddd
        }
    },
    test: testttt
}

DOM output

<ul>
    <li>
        face
        <ul>
            <li>
                <div>aaa</div>
                <div></div>
            </li>
            <li>
                <div>bbb</div>
                <div></div>
            </li>
            <li>
                <div>ccc</div>
                <div>cccc</div>
            </li>
            <li>
                more
                <ul>
                    <li>
                        <div>ddd</div>
                        <div>dddd</div>
                    </li>
                    <li>
                        <div>eee</div>
                        <div></div>
                    </li>
                </ul>
            </li>
        </ul>
    </li>
    <li>
        <div>magic</div>
        <div></div>
    </li>
    <li>
        <div>test</div>
        <div>testttt</div>
    </li>
</ul>

I'm asking this as an Angular question, but I plan on using vue.js. I do this because the Angular community is much bigger and because I believe concepts learnt from Angular are easily transposed onto vue.js. I don't want to use Angular itself, because a full framework is way more than I need.

Code example of vue.js redering an object as tree

Here is the repo for the project: https://github.com/jdwillemse/translation-utility

SamMorrowDrums

You can, if you use ng-repeat="(key, value) in master"

Then you can simply do:

{{translation[key]}}

This question answers how to do the basics of key value looping.

This is also building a tree in Ang

I haven't used Vue.js, but you should be able to do this by using key, value looping, and nesting key value loops inside.

This is the Vue equivalent:

<ul id="repeat-object"> <li v-repeat="primitiveValues">{{$key}} : {{$value}}</li> <li v-repeat="objectValues">{{$key}} : {{msg}}</li> </ul>

What I'm not sure about in Vue, is if you can access [] notation in the DOM. You want {{translation[$key]}}, not sure if you can access data like that in Vue. You can in Angular.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JS loop through objects

From Dev

JS loop through objects

From Dev

Loop through severals objects js

From Dev

AngularJS Directive to loop through array

From Java

forEach loop through two arrays at the same time in javascript

From Dev

Loop through an array of objects?

From Dev

Loop through Mongoose objects

From Dev

Loop through Mongoose objects

From Dev

Loop through objects React

From Dev

Loop through an object of objects

From Dev

Two different results for same JS,when ran through Browser's JS Console and through a JS File

From Dev

Two objects are drawn at the same time in Fabric.js

From Dev

Loop through the children of an element in a AngularJS directive

From Dev

Foreach loop through two data sets and use same loop index for both

From Dev

loop through two muti-dimensional array at same time using foreach loop

From Dev

Loop through nested objects with jQuery

From Dev

Loop through injected objects in a factory

From Dev

Objective C - Loop through NSArray where all objects inherit from same protocol

From Dev

Filter through two arrays of objects

From Dev

Loop through Array of Objects within Objects in JavaScript

From Dev

JavaScript--merge two array objects into one arr object then loop through each index/object

From Dev

Loop through multiple of same blocks of markup in JS and perform same function without interference

From Dev

SQL : loop through same table

From Dev

Run through a loop for more than 100,000 rows of data in two sheets in the same workbook

From Dev

Loop through two files Ruby

From Dev

FOR loop to iterate through two folders

From Dev

Nunit: Check that two objects are the same

From Dev

In Python, when are two objects the same?

From Dev

When are two objects the same in VBA?

Related Related

  1. 1

    JS loop through objects

  2. 2

    JS loop through objects

  3. 3

    Loop through severals objects js

  4. 4

    AngularJS Directive to loop through array

  5. 5

    forEach loop through two arrays at the same time in javascript

  6. 6

    Loop through an array of objects?

  7. 7

    Loop through Mongoose objects

  8. 8

    Loop through Mongoose objects

  9. 9

    Loop through objects React

  10. 10

    Loop through an object of objects

  11. 11

    Two different results for same JS,when ran through Browser's JS Console and through a JS File

  12. 12

    Two objects are drawn at the same time in Fabric.js

  13. 13

    Loop through the children of an element in a AngularJS directive

  14. 14

    Foreach loop through two data sets and use same loop index for both

  15. 15

    loop through two muti-dimensional array at same time using foreach loop

  16. 16

    Loop through nested objects with jQuery

  17. 17

    Loop through injected objects in a factory

  18. 18

    Objective C - Loop through NSArray where all objects inherit from same protocol

  19. 19

    Filter through two arrays of objects

  20. 20

    Loop through Array of Objects within Objects in JavaScript

  21. 21

    JavaScript--merge two array objects into one arr object then loop through each index/object

  22. 22

    Loop through multiple of same blocks of markup in JS and perform same function without interference

  23. 23

    SQL : loop through same table

  24. 24

    Run through a loop for more than 100,000 rows of data in two sheets in the same workbook

  25. 25

    Loop through two files Ruby

  26. 26

    FOR loop to iterate through two folders

  27. 27

    Nunit: Check that two objects are the same

  28. 28

    In Python, when are two objects the same?

  29. 29

    When are two objects the same in VBA?

HotTag

Archive