简体   繁体   English

JS - 在推入数组之前检查项目是否存在

[英]JS - Check item exist before Pushing in Array

I've unformatted JSON structured key-value pair data.我没有格式化 JSON 结构化键值对数据。 I need to format it and returned it into another formatted structured.so,我需要对其进行格式化并将其返回到另一种格式化的 structured.so 中,

Sample Code::示例代码::

// Unformatted data like this, which contains repeating keys

  let query = {
    "junk,fruit,vegetable,junk,fruit": "pizza,apple,potato,burger,mango"
  }


// formatting like this,
const keys = Object.keys(query)[0].split(",");
const values = Object.values(query)[0].split(",");

const newObj = {}

for (let i = 0; i < keys.length; i++) {
   newObj[keys[i]] = values[i]
}

console.log(newObj)

//[ junk:pizza and fruit:apple are not returned in console]

//Output:
// {junk:  'burger',
// fruit: 'mango',
// vegetable: 'potato'}

JSON, doesn't allow repeating keys that's why its not returned.That's why I'm trying to returned it in another structure. JSON,不允许重复键,这就是它不返回的原因。这就是为什么我试图在另一个结构中返回它。

For doing that, if the key is repeating then push its value into same data of array as shown in Expected Output.为此,如果重复,则将其value推送到数组的相同数据中,如预期 Output 所示。

newObj.includes('junk') or newObj.includes('mango') , with this, it can be checked if that particular key already present in output or not or in an array. newObj.includes('junk')newObj.includes('mango') ,可以检查该特定是否已存在于 output 中或是否存在于数组中。

I want to returned my Output: like this我想退回我的Output:像这样

{
    'junk': {
     'data': [
       'pizza', 
       'burger'
      ]
    }, 
    'fruit': {
     'data': [
       'apple',
       'mango'
     ]
    },
    'vegetable': {
     'data': [
        'potato'
      ]
    }
}        

JSFiddle Link: https://jsfiddle.net/sophia22134/0yLxowt4/ JSFiddle 链接: https://jsfiddle.net/sophia22134/0yLxowt4/

This might help这可能有帮助

If(keyArray.indexOd(i) == -1) { valueArray.push(i) }

Or just need to update或者只需要更新

const newValueKey = {};

for (let i = 0; i < keys.length; i++) {
  if (!newValueKey[keys[i]]) newValueKey[keys[i]] = { data: [] };
  newValueKey[keys[i]].data.push(values[i]);
}

Only need to conditionally assign the value as an array or concat it with the existing value depending if the value on key exists.只需要有条件地将值分配为数组或将其与现有值连接,具体取决于键上的值是否存在。

For simplicity, if the key will only contain an array of string it is not necessary nesting it into another object: newObj.fuits={data:[...]} should be newObj.fruits=[...]为简单起见,如果键只包含一个字符串数组,则没有必要将其嵌套到另一个 object 中: newObj.fuits={data:[...]}应该是newObj.fruits=[...]

 // Unformatted data like this, which contains repeating keys let query = { "junk,fruit,vegetable,junk,fruit": "pizza,apple,potato,burger,mango" } // formatting like this, const keys = Object.keys(query)[0].split(","); const values = Object.values(query)[0].split(","); const newObj = {} for (let i = 0; i < keys.length; i++) { let key=keys[i] let value=values[i] newObj[key] = newObj[key]? [...newObj[key],value]: [value] } console.log(newObj) /* { "junk": [ "pizza", "burger" ], "fruit": [ "apple", "mango" ], "vegetable": [ "potato" ] } */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM