Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> wyszukiwanie w array
dvdx95
post
Post #1





Grupa: Zarejestrowani
Postów: 47
Pomógł: 3
Dołączył: 11.10.2016

Ostrzeżenie: (0%)
-----


var prices;
request('prices.txt', function(error, response, body) {
var prices = JSON.parse(body);
var name = wgg[i].name; -- jak zrobie console.log to pokazuje poprawną wartość
teraz chce wyciągnąć

var cost = parseFloat(prices.response.items.name.value/100); - tutaj name jest już undefined dlaczego?


});

Pytanie

Jak wrzucić wartość z var name do tego prices.response.items.name.value

Przykład
moje var name = skrzynka
chce aby var cost = prices.response.items.skrzynka.value
a wychodzi tak prices.response.items.undefined

Ten post edytował dvdx95 31.07.2017, 20:21:22
Go to the top of the page
+Quote Post
olszam
post
Post #2





Grupa: Zarejestrowani
Postów: 342
Pomógł: 23
Dołączył: 20.01.2011
Skąd: Chełm

Ostrzeżenie: (0%)
-----


Kod
prices.response.items.name.value = name
Go to the top of the page
+Quote Post
markuz
post
Post #3





Grupa: Zarejestrowani
Postów: 1 240
Pomógł: 278
Dołączył: 11.03.2008

Ostrzeżenie: (0%)
-----


A prices.response.items to nie tablica? Pokaż wynik console.log a najlepiej pokaż body (w formacie json).
Go to the top of the page
+Quote Post
Pyton_000
post
Post #4





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


a tak:

Kod
var cost = parseFloat(prices.response.items[name].value/100);
Go to the top of the page
+Quote Post
dvdx95
post
Post #5





Grupa: Zarejestrowani
Postów: 47
Pomógł: 3
Dołączył: 11.10.2016

Ostrzeżenie: (0%)
-----


body wygląda tak

{"resonse":"1"{"items":{"skrzynka":{"value":10}}}}

var cost = parseFloat(prices.response.items[name][value]/100);
Sprawdzę tak i dam znać.

Ten post edytował dvdx95 1.08.2017, 11:26:18
Go to the top of the page
+Quote Post
markuz
post
Post #6





Grupa: Zarejestrowani
Postów: 1 240
Pomógł: 278
Dołączył: 11.03.2008

Ostrzeżenie: (0%)
-----


To jest niepoprawny JSON

Kod
JSON.parse('{"resonse":"1"{"items":{"skrzynka":{"value":"10"}}}');
SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 1 column 15 of the JSON data [Więcej informacji]


Czytaj logi, bez tego cieżko cokolwiek napisać.

Ten post edytował markuz 1.08.2017, 11:17:32
Go to the top of the page
+Quote Post
dvdx95
post
Post #7





Grupa: Zarejestrowani
Postów: 47
Pomógł: 3
Dołączył: 11.10.2016

Ostrzeżenie: (0%)
-----


złe po prostu wpisałem zapomniałem dodać } na końcu, JSON jest poprawny {"resonse":"1"{"items":{"skrzynka":{"value":10}}}}

Ten post edytował dvdx95 1.08.2017, 11:26:06
Go to the top of the page
+Quote Post
markuz
post
Post #8





Grupa: Zarejestrowani
Postów: 1 240
Pomógł: 278
Dołączył: 11.03.2008

Ostrzeżenie: (0%)
-----


Znów podałeś niepoprawny - nie możesz zrobić kopiuj wklej?
Go to the top of the page
+Quote Post
dvdx95
post
Post #9





Grupa: Zarejestrowani
Postów: 47
Pomógł: 3
Dołączył: 11.10.2016

Ostrzeżenie: (0%)
-----


{"response":{"success":1,"current_time":1500649142,"items":{"Chroma Case":{"last_updated":1500646124,"quantity":65,"value":1193}}}}
Go to the top of the page
+Quote Post
markuz
post
Post #10





Grupa: Zarejestrowani
Postów: 1 240
Pomógł: 278
Dołączył: 11.03.2008

Ostrzeżenie: (0%)
-----


@Pyton_000 podał poprawną odpowiedź, tylko wtedy gdy name = 'Chroma Case'.

Cytat
var name = wgg[i].name; -- jak zrobie console.log to pokazuje poprawną wartość

Co to poprawna wartość? 'Chroma Case'?
Go to the top of the page
+Quote Post
dvdx95
post
Post #11





Grupa: Zarejestrowani
Postów: 47
Pomógł: 3
Dołączył: 11.10.2016

Ostrzeżenie: (0%)
-----


tak dokładnie pokazuje var name = "Chroma Case"
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 23.08.2025 - 11:06