Форум Flasher.ru

Форум Flasher.ru (http://www.flasher.ru/forum/index.php)
-   ActionScript 3.0 (http://www.flasher.ru/forum/forumdisplay.php?f=83)
-   -   xml. получение значений без повторений (http://www.flasher.ru/forum/showthread.php?t=134242)

Felicast 24.12.2009 17:25

xml. получение значений без повторений
 
в общем ситуация такая:
есть xml файл вида:
Код:

<response>
    <item>
        <id>1</id>
        <cost>150</cost>
        <period>1</period>
    </item>
    <item>
        <id>1</id>
        <cost>100</cost>
        <period>2</period>
    </item>
    <item>
        <id>2</id>
        <cost>70</cost>
        <period>1</period>
    </item>
    <item>
        <id>2</id>
        <cost>90</cost>
        <period>2</period>
    </item>
</response>

нужно бы получить все уникальные занчения item.id, т.е. для данного примера - значения 1, 2
выражение:
Код AS3:

for each (var id:XML in response.item.id)
{
    trace(id);
}

выводит:
Код:

1
1
2
2

а как бы мне красивым путем получить значения без повторений?

wvxvw 24.12.2009 17:30

Код AS3:

var xml:XML = 
<response>
    <item>
        <id>1</id>
        <cost>150</cost>
        <period>1</period>
    </item>
    <item>
        <id>1</id>
        <cost>100</cost>
        <period>2</period>
    </item>
    <item>
        <id>2</id>
        <cost>70</cost>
        <period>1</period>
    </item>
    <item>
        <id>2</id>
        <cost>90</cost>
        <period>2</period>
    </item>
</response>;
var d:Dictionary = new Dictionary();
 
var list:XMLList = xml.item.id.(!d[valueOf().text().toString()] &&
                                (d[valueOf().text().toString()] = true));
trace(list.toXMLString());
/*
<id>1</id>
<id>2</id>
*/


Felicast 24.12.2009 17:52

спасибо работает.
первое, что в голову пришло - тоже использовать Dictionary, правда не так элегантно


Часовой пояс GMT +4, время: 13:14.

Copyright © 1999-2008 Flasher.ru. All rights reserved.
Работает на vBulletin®. Copyright ©2000 - 2026, Jelsoft Enterprises Ltd. Перевод: zCarot
Администрация сайта не несёт ответственности за любую предоставленную посетителями информацию. Подробнее см. Правила.