Skip to main content
  1. About
  2. For Teams
Asked
Modified 11 years ago
Viewed 105 times
0

I have this

JS

var invoice = 1;

var orders = [
    [{
        "invoice": "1",
        "article": "Coca Cola",
        "qty": "5",
        "active": "Yes"
    }, {
        "invoice": "1",
        "article": "Fanta",
        "qty": "10",
        "active": "Yes"
    }, {
        "invoice": "1",
        "article": "Sprite",
        "qty": "40",
        "active": "Yes"
    },{
        "invoice": "1",
        "article": "Coca Cola",
        "qty": "40",
        "active": "Yes"
    }],
    [{
        "invoice": "2",
        "article": "Coca Cola",
        "qty": "55",
        "active": "Yes"
    }, {
        "invoice": "2",
        "article": "Fanta",
        "qty": "10",
        "active": "Yes"
    }]
];

var msg = "<div id=results>" + results + "</div>";

and final results in html to look like this lets say for invoice 1, to count all articles with same name all its qty, and display that article name with total amount of the qty. Here is html how it need to be display?

<div id="results">
<div class="each">
<input value="Coca Cola"><input value="45">
</div>
<div class="each">
<input value="Fanta"><input value="10">
</div>
<div class="each">
<input value="Sprite"><input value="40">
</div>

On Coca Cola i have to display countet values of qty? I have starded a working fiddle, but as you may see i have stucked at start.

9
  • It would probably be clearer and easier to understand if the values in the HTML actually matched the posted object ?
    adeneo
    –  adeneo
    2014-10-02 20:03:04 +00:00
    Commented Oct 2, 2014 at 20:03
  • That is no JSON. At Invoice the numbers are missing "".
    loveNoHate
    –  loveNoHate
    2014-10-02 20:03:06 +00:00
    Commented Oct 2, 2014 at 20:03
  • @adeneo He wants to add the 2 Coca Cola values together and display the rest as is
    loveNoHate
    –  loveNoHate
    2014-10-02 20:03:48 +00:00
    Commented Oct 2, 2014 at 20:03
  • 2
    @DOCASAREL: a) Number literals are valid in JSON. b) Of course this isn't JSON, this is a JavaScript array. The context matters.
    Felix Kling
    –  Felix Kling
    2014-10-02 20:04:46 +00:00
    Commented Oct 2, 2014 at 20:04
  • Yes there are matched in html
    Miomir Dancevic
    –  Miomir Dancevic
    2014-10-02 20:05:10 +00:00
    Commented Oct 2, 2014 at 20:05

1 Answer 1

2

Here's one way to do it

function getInvoice(numb) {
    return $('<section />', {
        'class' : 'results'
    }).append(
        $.map(orders, function(arr) {
            var o = {};
            $.each(arr, function(_, obj) {
                if (obj.invoice == numb)
                    o[obj.article] = (o[obj.article] || 0) + (+obj.qty);
            });

            return $.map(o, function(qty, article) {
                return $('<div />', {
                    'class' : 'each'
                }).append(
                    $('<input />', {value : article}),
                    $('<input />', {value : qty})
                ).get(0);
            });
        })
    );
}

FIDDLE

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

5 Comments

Nice this is great can you correct it because i have to make is print like this var show ="<section>" + msg + "</section>;
Sure, I suppose you just change the container from div to section, like above ?
Sorry mate stil struglling to output, i need this html <div class='col-lg-6'> <p>Article</p> <input class='form-control' value='' name='whatArticle' disabled type='text'> </div> <div class='col-lg-6'> <p>Quantity</p> <input class='form-control' value='' name='whatQty' disabled type='text'> </div>
Well, that looks completely different from the question, but something like this maybe -> jsfiddle.net/bs80wn88/1
Txanks mate, your are great

Your Answer

Post as a guest

Required, but never shown

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.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.