将JavaScript字符串更新为数组

时间:2019-10-16 10:33:05

标签: javascript arrays string

我在下面也有一个包含一堆参数的URL;

conditions=[{"name”:”foo”,”operator”:”bar”,”value":350000}]

这也可能是以下

conditions=[]

conditions=[{"name”:”one”,”operator”:”two”,”value":350000}, {"name”:”three”,”operator”:”one”,”value”:22}]

理论上也可以是

conditions=[{"name”:”one”,”operator”:”two”,”value":350000}, {"name”:”three”,”operator”:”one”,”value”:22}, {"name”:”four”,”operator”:”two”,”value”:22}, {"name”:”sixty”,”operator”:”three”,”value”:22}, ..]

基于输入,我想更改条件= []。我的代码中包含以下变量

inputName, obj, valueOperator

这些可能包含以下值:

inputName = “three”
valueOperator = “two”
obj.value = 22

因此,据此我可以像前面描述的那样构造条件。现在到我的问题;

我想分解conditions=[]并检查方括号中是否有任何内容,如果没有,我将添加新条件。如果有什么东西,我想检查inputName是否与括号中的任何“名称” 相匹配。如果没有,我将添加新条件。如果匹配,我想用新变量更新值和运算符。完成后,我想重新整理conditions=[]。随着更新的条件。一次总是只会更新一个条件。

我迷路了。有人有个好主意吗?

4 个答案:

答案 0 :(得分:0)

如果您具有以下url

https://example.com/myfolder/?conditions=[{"name”:”foo”,”operator”:”bar”,”value":350000}]

然后,queryString参数conditions的值是一个JSON字符串。

要将该值转换为javascript object,您需要执行以下操作:

// CAPTURE THE QUERY STRING IN A URLSearchParams OBJECT INSTANCE
let urlParams = new URLSearchParams(window.location.search);

// GET THE RELEVANT JSON STRING FROM urlParams
let conditionsJSON = urlParams.get('conditions');

// CONVERT THE JSON STRING INTO A JAVASCRIPT OBJECT
let conditionsObject = JSON.parse(conditionsJSON);

您现在将看到一个arrayconditionsObject),如下所示:

[
  {
    name : "foo",
    operator : "bar",
    value : 350000
  }
]

NB 为避免混淆,在JavaScript中,arrayobject特定种类-因此,上面的代码同时表示了{ {1}} 一个array(因为前者代表后者的子集)。

答案 1 :(得分:0)

此代码应该有效

const inputName = 'foo'
const valueOperator = 'two'
const value = 22

const conditionString = 'condition=[{"name”:”foo”,”operator”:”bar”,”value":350000}]'
const data = JSON.parse(conditionString.substr(10).replace(/”/g, '"'))

const newData = { name: inputName, operator: valueOperator, value: value }
let ind = data.findIndex(v => v.name === inputName) 
if (ind < 0) ind = data.length

data[ind] = newData
const newConditionsString = `conditions=${JSON.stringify(data)}`
console.log(newConditionsString)

答案 2 :(得分:0)

根据您提供的说明,我尝试复制它。希望这行得通。

const inputName = 'three';
const valueOperator = 'two';
const obj = 22;

let conditions = [
  { name: 'one', operator: 'two', value: 350000 },
  { name: 'two', operator: 'one', value: 22 }
];

if (conditions.length == 0) {
  conditions.push({ name: inputName, operator: valueOperator, value: obj });
} else {
  let found = false;
  for (const condition of conditions) {
    if (condition.name == inputName) {
      condition.operator = valueOperator;
      condition.value = obj;
      found = true; // mark found as true when name found
    }
  }

  // Insert values if not found in conditions
  if (!found) {
    conditions.push({ name: inputName, operator: valueOperator, value: obj });
  }
}

console.log(conditions);

答案 3 :(得分:0)

您的问题的第一部分可以通过Rounin提供的解决方案来解决。对于要基于名称进行条件匹配的第二部分,下面的代码应该可以为您提供帮助。

inputName="Three"
valueOperator = "two"
value = 22

if(conditionsObject.length!=0){
    let index = conditionsObject.findIndex((item)=>item.name==inputName)
    if(index!=-1){
        conditionsObject[index].value = value
        conditionsObject[index].operator = valueOperator
    }
    else{
    conditionsObject.push({ name: inputName, operator: valueOperator, value: value })
    }
else{
    conditionsObject.push({ name: inputName, operator: valueOperator, value: value })
}
}