使用JS中的JSON对象值动态更新String

问题描述 投票:0回答:1

我具有此功能,可以在其中创建请求并存储数据。 (存储为OBJ)

再次发出请求以获取新的URL进行调用。此URL需要我更新url,以包含来自存储数据的值。

正在保存的数据:

{
  "location": {
    "ip": "78.152.221.20",
    "success": true,
    "type": "IPv4",
    "continent": "Europe",
    "continent_code": "EU",
    "country": "Ireland",
    "country_code": "IE",
    "country_flag": "https://cdn.ipwhois.io/flags/ie.svg",
    "country_capital": "Dublin",
    "country_phone": "+353",
    "country_neighbours": "GB",
    "region": "County Dublin",
    "city": "Swords",
    "latitude": "53.4557467",
    "longitude": "-6.2197406",
    "asn": "AS15502",
    "org": "Vodafone Ireland",
    "isp": "Vodafone Ireland Limited",
    "timezone": "Europe/Dublin",
    "timezone_name": "Greenwich Mean Time",
    "timezone_dstOffset": "0",
    "timezone_gmtOffset": "0",
    "timezone_gmt": "GMT 0:00",
    "currency": "Euro",
    "currency_code": "EUR",
    "currency_symbol": "€",
    "currency_rates": "0.926884",
    "currency_plural": "euros",
    "completed_requests": 49
  }
}

第二个请求返回此URL,我需要更新:

"url": "https://api.sunrise-sunset.org/json?lat={{ location.latitude }}&lng={{ location.longitude }}"

我试图创建一个处理字符串并返回的函数。一旦返回,将设置一个字符串模板文字,如下所示:

`https://api.sunrise-sunset.org/json?lat=${OBJ['location']['latitude']&lng=${OBJ['location']['longitude']`

但是它不起作用,它只是使用字符串,而不采用先前存储为OBJ的值。

这是我的代码,任何建议或指针或任何东西都将不胜感激。

const interpolateURLorMessage = (string) => {
  if (CONST.DEBUG) {
    console.log('functions.checkURLorMessage: string:', string);
  }

  if (!string) {
    return null;
  }

  if (string.includes('{{') && string.includes('}}')) {
    const myRegexp = /\{{(.*?)\}}/g;
    const matches = string.match(myRegexp);

    let newURL = `${string}`;
    matches.forEach((ele) => {
      const match = ele;
      let newMatch = ele.replace('.', '\'][\'');
      newMatch = newMatch.replace('{{', '${OBJ[\'');
      newMatch = newMatch.replace('}}', '\']');
      newMatch = newMatch.replace(/\s/g, '');
      newURL = newURL.replace(match, newMatch);
    });

    return newURL;
  } else {
    // Noting to interpolate
    return string;
  }
};
javascript json npm ecmascript-6 string-literals
1个回答
0
投票
但是这将适用于您那里的简单用例。

var OBJ={};CONST={DEBUG:true}; const interpolateURLorMessage = (string) => { if (CONST.DEBUG) { console.log('functions.checkURLorMessage: string:', string); } if (!string) { return null; } if (string.includes('{{') && string.includes('}}')) { return string.replace(/\{{(.*?)\}}/g,(r,p1)=>p1.trim().split('.').reduce((acc,p)=>acc[p],OBJ)); } else { // Noting to interpolate return string; } }; var urlobj = {"url": "https://api.sunrise-sunset.org/json?lat={{ location.latitude }}&lng={{ location.longitude }}"}; var data = { "location": { "ip": "78.152.221.20", "success": true, "type": "IPv4", "continent": "Europe", "continent_code": "EU", "country": "Ireland", "country_code": "IE", "country_flag": "https://cdn.ipwhois.io/flags/ie.svg", "country_capital": "Dublin", "country_phone": "+353", "country_neighbours": "GB", "region": "County Dublin", "city": "Swords", "latitude": "53.4557467", "longitude": "-6.2197406", "asn": "AS15502", "org": "Vodafone Ireland", "isp": "Vodafone Ireland Limited", "timezone": "Europe/Dublin", "timezone_name": "Greenwich Mean Time", "timezone_dstOffset": "0", "timezone_gmtOffset": "0", "timezone_gmt": "GMT 0:00", "currency": "Euro", "currency_code": "EUR", "currency_symbol": "€", "currency_rates": "0.926884", "currency_plural": "euros", "completed_requests": 49 } }; OBJ=data; console.log( interpolateURLorMessage(urlobj.url) );
© www.soinside.com 2019 - 2024. All rights reserved.