जावास्क्रिप्ट सीखना। मुझे कुछ डेटा प्लॉट करने की ज़रूरत है (मैं निवो का उपयोग कर रहा हूं)।

मेरे पास मेरे एपीआई से प्रारूप में डेटा है:

[{"as_at_date":"2020-02-21","value":815202269.5,"num":139},{"as_at_date":"2020-02-25","value":809209302.32,"num":139},{"as_at_date":"2020-03-12","value":723686212.35,"num":139},{"as_at_date":"2020-03-13","value":734798809.24,"num":139},{"as_at_date":"2020-03-16","value":701366943.2,"num":139},{"as_at_date":"2020-03-17","value":710833514.89,"num":139},{"as_at_date":"2020-03-18","value":699154469.83,"num":139},{"as_at_date":"2020-03-19","value":694649684.34,"num":139},{"as_at_date":"2020-03-20","value":685400033.9,"num":139}]

निवो के साथ एक लाइन चार्ट तैयार करने के लिए मुझे उपरोक्त सरणी को इसमें बदलना होगा:

    [{"id": 'XXXXX",
"data": [{"x":"2020-02-21","y":815202269.5},{"x":"2020-02-25","y":809209302.32},{"x":"2020-03-12","y":723686212.35},{"x":"2020-03-13","y":734798809.24},{"x":"2020-03-16","y":701366943.2},{"x":"2020-03-17","y":710833514.89},{"x":"2020-03-18","y":699154469.83},{"x":"2020-03-19","y":694649684.34},{"x":"2020-03-20","y":685400033.9}]
  • "संख्या" संपत्ति पर ध्यान न दें।
  • डेटा घोंसला और एक "आईडी" बनाएं
  • as_at_date को "x" में बदलें, मान को "y" में बदलें

अंतिम प्रयास .map जैसे .map(item => [{"x": item['as_at_date']}, {"y": item['value']}]) का उपयोग करने का प्रयास कर रहा था, लेकिन बिल्कुल सही नहीं था।

किसी भी मदद की सराहना करें, धन्यवाद!

0
swifty 23 मार्च 2020, 12:43

4 जवाब

map निश्चित रूप से जाने का रास्ता है।

यहाँ मेरा दृष्टिकोण है:

const data = [{"as_at_date":"2020-02-21","value":815202269.5,"num":139},{"as_at_date":"2020-02-25","value":809209302.32,"num":139},{"as_at_date":"2020-03-12","value":723686212.35,"num":139},{"as_at_date":"2020-03-13","value":734798809.24,"num":139},{"as_at_date":"2020-03-16","value":701366943.2,"num":139},{"as_at_date":"2020-03-17","value":710833514.89,"num":139},{"as_at_date":"2020-03-18","value":699154469.83,"num":139},{"as_at_date":"2020-03-19","value":694649684.34,"num":139},{"as_at_date":"2020-03-20","value":685400033.9,"num":139}];

function reformattedData(arr) {

  // Destructure only the required properties
  return arr.map(({ as_at_date, value }) => {

    // And just return those values in a new object
    // using the new property names
    return { x: as_at_date, y: value };
  });
}

const out = { id: 'xxx', data: reformattedData(data) };

console.log(out);
0
Andy 23 मार्च 2020, 12:49

आपका प्रयास लगभग सही था, बस आसपास के [] को छोड़ दें:

const input = [{"as_at_date":"2020-02-21","value":815202269.5,"num":139},{"as_at_date":"2020-02-25","value":809209302.32,"num":139},{"as_at_date":"2020-03-12","value":723686212.35,"num":139},{"as_at_date":"2020-03-13","value":734798809.24,"num":139},{"as_at_date":"2020-03-16","value":701366943.2,"num":139},{"as_at_date":"2020-03-17","value":710833514.89,"num":139},{"as_at_date":"2020-03-18","value":699154469.83,"num":139},{"as_at_date":"2020-03-19","value":694649684.34,"num":139},{"as_at_date":"2020-03-20","value":685400033.9,"num":139}];

const outputData = input.map(item => ({ x: item.as_at_date, y: item.value }));

console.log(outputData);

const result = {
  id: 'XXXX', // create your id here,
  data: outputData,
};
console.log(result);
0
cyr_x 23 मार्च 2020, 12:50

आप इसे आजमा सकते हैं

const array = [{"as_at_date":"2020-02-21","value":815202269.5,"num":139},{"as_at_date":"2020-02-25","value":809209302.32,"num":139},{"as_at_date":"2020-03-12","value":723686212.35,"num":139},{"as_at_date":"2020-03-13","value":734798809.24,"num":139},{"as_at_date":"2020-03-16","value":701366943.2,"num":139},{"as_at_date":"2020-03-17","value":710833514.89,"num":139},{"as_at_date":"2020-03-18","value":699154469.83,"num":139},{"as_at_date":"2020-03-19","value":694649684.34,"num":139},{"as_at_date":"2020-03-20","value":685400033.9,"num":139}];

const formatted_array = input.map((item) => {
return {x: item.as_at_date, y: item.value}});

const final_data = {
  id: 'XXXX',
  data: formatted_array ,
};
console.log(final_data);
0
JDC 23 मार्च 2020, 12:54